Go to Post Corndog is the new water game. - Basel A [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-02-2008, 16:11
CrazyNorman CrazyNorman is offline
Registered User
FRC #1100
 
Join Date: Feb 2008
Location: Northborough, MA
Posts: 2
CrazyNorman is an unknown quantity at this point
Want Malloc on FIRST?

As a preface, I am aware that it is generally bad practice to use dynamic memory allocation in embedded code. On the other hand, there are certain things you 100% need it for, and if used sparingly, it can be useful.

For those of you who want dynamic memory allocation on your program, I wrote an allocator, which can be used similarly to malloc/free. It reserves a total of 1024 bytes, within which it can dynamically allocate memory for you. You can increase or decrease this amount with some minor tweaks.

Every allocation you perform has an overhead of 4 bytes, e.x., if you allocate a ten byte structure, 14 bytes of memory are used. An allocation requires time linearly proportional to the number of currently allocated blocks, and deallocation takes place in roughly constant time. For real world use, the amount of time taken in either situation is negligible, and might even be usable within an ISR (although I have not tried it). Freed blocks which can be recombined are automatically recombined to prevent fragmentation.

It provides two functions:
Code:
/* Include ElevenAlloc.h to access these functions.  */
void *elevenAlloc(unsigned int amount);
void elevenDealloc(void* memPtr);

/* For example, to allocate ten ints, you could do */
int *myArray = elevenAlloc(sizeof(int) * 10);

/* Make sure you free them later to prevent memory leaks */
elevenDealloc(myArray);
Just add ElevenAlloc.h and ElevenAlloc.c to your project to revolutionize your life. The code is licensed under the BSD license, so you can use it within your project for free, but you may not remove the copyright notice.
Attached Files
File Type: c ElevenAlloc.c (5.4 KB, 53 views)
File Type: h ElevenAlloc.h (1.7 KB, 62 views)
  #2   Spotlight this post!  
Unread 07-02-2008, 23:10
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,815
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: Want Malloc on FIRST?

Now, i've been coding for many, many years, and i've gotta say... i can't think of a single situation in FRC that you would 100% need dynamic allocation for... care to share a few examples that i may have overlooked?
  #3   Spotlight this post!  
Unread 08-02-2008, 17:10
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,170
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Re: Want Malloc on FIRST?

"i can't think of a single situation in FRC that you would 100% need dynamic allocation for"

... me either, this is not a great idea.
  #4   Spotlight this post!  
Unread 09-02-2008, 16:19
3dude_2231's Avatar
3dude_2231 3dude_2231 is offline
no one messes with a Thingy =|
AKA: Menscher,Ran Menscher =]
FRC #2231 (Onyxtronix)
Team Role: Leadership
 
Join Date: Feb 2007
Rookie Year: 2007
Location: Shoham, Israel
Posts: 233
3dude_2231 is a jewel in the rough3dude_2231 is a jewel in the rough3dude_2231 is a jewel in the rough
Send a message via MSN to 3dude_2231
Re: Want Malloc on FIRST?

I love the idea,
even though it's not as useful (as mentioned above)

must have been challenging and fun..

Ran.
__________________
Redefining the word "Rookie". (for 2 years now..)


Israeli Off Season Games: WINNERS!
check out this cool project I'm into..

  #5   Spotlight this post!  
Unread 11-02-2008, 21:35
CrazyNorman CrazyNorman is offline
Registered User
FRC #1100
 
Join Date: Feb 2008
Location: Northborough, MA
Posts: 2
CrazyNorman is an unknown quantity at this point
Re: Want Malloc on FIRST?

Sorry, I exaggerated when I said that dynamic allocation would be 100% necessary, as dynamic allocation can be written in terms of static allocation (as here). On the other hand, having a dynamic allocation system for certain things can result in cleaner code than without.

For example, lets say that in your autonomous, you want to watch out for nearby obstacles such as robots. When the rangefinder picks them up, you could store their location relative, a time, etc. Then, as objects are no longer detected for a certain amount of time, pass out of range, etc., you delete them. Now, you could have a stack, find the first unused "object", store in it, loop through them, make sure they are within range, mark them as unused, etc.

Of course, now you just wrote a local dynamic allocator for that section of code, and when another part of your code works upon semi-similar principles, you just have to rewrite it again. Yet another chance for duplication, mistakes, and horrors.

On the other hand, you could have a generic allocator which works consistently, and use it everywhere that it results in cleaner code than the custom written stack. Don't use this everywhere, but I'm sorry, static variables everywhere and custom stacks don't necessarily result in more bug- free code.
  #6   Spotlight this post!  
Unread 12-02-2008, 02:14
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: Want Malloc on FIRST?

Quote:
Originally Posted by eagle33199 View Post
Now, i've been coding for many, many years, and i've gotta say... i can't think of a single situation in FRC that you would 100% need dynamic allocation for... care to share a few examples that i may have overlooked?
There are plenty of reasons, like the one outlined above, either way if it helps programmers right more elegant code then even if it is not 100% necessary it is still a great tool. Props on writing this.
I think the O(n) insert time and the 4byte overhead needs to be improved, can you think of any ideas? This looks like a fun project. Another cool idea could be to implement a vector for storing generic data...
__________________
Team 701
  #7   Spotlight this post!  
Unread 12-02-2008, 11:49
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,815
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: Want Malloc on FIRST?

Quote:
Originally Posted by CrazyNorman View Post
For example, lets say that in your autonomous, you want to watch out for nearby obstacles such as robots. When the rangefinder picks them up, you could store their location relative, a time, etc. Then, as objects are no longer detected for a certain amount of time, pass out of range, etc., you delete them. Now, you could have a stack, find the first unused "object", store in it, loop through them, make sure they are within range, mark them as unused, etc.
And yet a simpler way of doing that might be to create an array at runtime that represents the space around you, and when you detect something store a loop counter (representing time) in the corresponding space in the array. You automatically overwrite older locations, which isn't a problem, and you only 0 something out when you actually care about whats in that space (because you're moving there, lets say)... There are some extremely efficient algorithms you can use to move the array and rotate it and such as your robot moves (trust me, i've used them all before for graphics processing).

In the end, there are other ways to do it that are just as simple (or simpler) and just as elegant, but don't carry the negatives of attempting to use dynamically allocated memory in a situation where you have extremely limited resources.
  #8   Spotlight this post!  
Unread 12-02-2008, 16:58
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: Want Malloc on FIRST?

What you outline is a simple way implementing dynamic memory... why hack together something like that everytime you need to do something.
What he has done is basically provide a general way of doing what you just outlined that works for any datatype.
__________________
Team 701
  #9   Spotlight this post!  
Unread 12-02-2008, 17:23
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,170
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Re: Want Malloc on FIRST?

This a cool academic exercise but since FIRST is about real-world practices, I'll add in another 2 cents.

"For example, lets say that in your autonomous, you want to watch out for nearby obstacles such as robots. When the rangefinder picks them up, you could store their location relative, a time, etc. Then, as objects are no longer detected for a certain amount of time, pass out of range, etc., you delete them. Now, you could have a stack, find the first unused "object", store in it, loop through them, make sure they are within range, mark them as unused, etc."

Such a data structure would be fixed in size. In a real-time embedded environment, this would never be dynamically allocated (except maybe the entire memory area needed and then only during startup). One would set up an array of these objects and build a simple library to "allocate" them and "return" them to the pool. This would be a constant order operation. Non-deterministic functions (like malloc) are not a good idea in time sensitive software/firmware. Random-sized dynamic allocations can't be done deterministically (easily) because any algorithm to traverse the free-list is O(f(n)) and must be protected using mutual exclusion. This is the case in every OS I've ever used (and that is a long list).

HTH
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc? Jake M Programming 5 21-06-2007 21:24
Malloc? interfect Programming 6 05-02-2007 09:15
Do you want to Volunteer with FIRST? dez250 General Forum 13 18-11-2004 10:03
Colleges Want FIRST Students Brandon Martus Announcements 1 11-01-2004 23:50
Malloc, etc... rwaliany Programming 6 05-01-2004 02:18


All times are GMT -5. The time now is 19:12.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi