Go to Post Is it bad when are you just sitting at home wishing KICKOFF was here. - JulieB [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

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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, 52 views)
File Type: h ElevenAlloc.h (1.7 KB, 61 views)
 


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 23:48.

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