Documente Academic
Documente Profesional
Documente Cultură
Log in
Forums
Posts
Page Extras
Menu
Home All Forums [Development Tools] MPLAB C18 Compiler Dynamic memory allocation
Prev Thread
Next Thread
>
Showing page 1 of 2
Author
rbruce67
Essentials Only
Full Version
Post
Is dynamic memory allocation possible in C18? I notice a lack of functions in the *alloc
family, and that suggests "NO". Also, I don't see anything in the C18 manual or the C18
New Member
Libraries doc about memory allocation. I'm converting some microprocessor "c" code for
use on the PIC18F, and having dynamic allocation would save a bunch me rewriting time.
Total Posts : 10
Rew ard points : 0
Joined: 1/30/2004
Location: Kansas City, MO
Status: offline
Rob Bruce
Senior Engineer
Zeppelinware
www.zeppelinware.com
#1
23 Replies
Related Threads
how to find the memory profile from compiler compiled files, and
Help
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 1:03 AM (permalink)
http://www.microchip.com/forums/m114990.aspx
1/10
24/05/2016
Just take the time to appreciate that the RAM 'memory' in a Harvard architecture like the
PIC is very different than other systems' RAM memory. In the PIC, the RAM is more like a
very large CPU register file (hence the term 'file register'). You never allocate this RAM for
placing program code, as in other systems. Program memory, when using a PIC18 in
microcontroller mode, is always FLASH memory or 'ROM'-like. Doing a malloc() in flash
makes little sense. The linker also does only static allocation due to very limited internal
RAM that you generally find on a PIC.
Now, if you are talking about external memory bus equipped models, then it's another
story. Perhaps you can implement malloc() and move compiled code around in external
memory, if the code is entirely runtime relocatable. Most C functions are.
#2
jtemples
RE: Dynamic memory allocation Thursday, September 22, 2005 1:23 AM (permalink)
malloc() has nothing to do with executable code; it is used for dynamic data memory
allocation.
#3
Super Member
jtemples
RE: Dynamic memory allocation Thursday, September 22, 2005 1:24 AM (permalink)
It's possible, but you're on your own; malloc() is not provided with the library.
Super Member
#4
rbruce67
RE: Dynamic memory allocation Thursday, September 22, 2005 1:44 AM (permalink)
http://www.microchip.com/forums/m114990.aspx
2/10
24/05/2016
I have been savoring the Harvard architecture for a while now, with its tasty divided data
and program space, and I believe that I am ready to consume some data memory at last.
The PIC I'm targetting (18F6722) has 3938 Bytes of data RAM, which seems almost
Rob Bruce
Senior Engineer
Zeppelinware
www.zeppelinware.com
#5
rbruce67
RE: Dynamic memory allocation Thursday, September 22, 2005 1:50 AM (permalink)
Thanks. At least I'm not looking for something that's not there. If I'm successful in
creating malloc and free, I'll post them for the greater good.
New Member
Rob Bruce
Senior Engineer
Zeppelinware
Total Posts : 10
www.zeppelinware.com
#6
Joined: 1/30/2004
Location: Kansas City, MO
Status: offline
magio
RE: Dynamic memory allocation Thursday, September 22, 2005 6:52 AM (permalink)
ORIGINAL: rbruce67
0
Thanks. At least I'm not looking for something that's not there. If I'm
successful in creating malloc and free, I'll post them for the greater good.
Super Member
malloc() functions as i understand it... requires an OS that will handle the 'free' of it.
Total Posts : 2164
Rew ard points : 0
Status: offline
You don't have an OS on the PIC, I think i've seen malloc () mentioned on some 'fast
reading' on some places where they talk about OSes for PIC18xxx devices...
#7
jtemples
RE: Dynamic memory allocation Thursday, September 22, 2005 7:01 AM (permalink)
http://www.microchip.com/forums/m114990.aspx
3/10
24/05/2016
All malloc does is give you a pointer to a chunk of memory of the specified size. free()
returns that chunk of memory to the pool so a subsequent call to malloc() can reuse it.
You don't need an OS to do this. The reason it doesn't show up in C compilers for small
micros is that it's generally just not practical to use. When people want it, it's usually
Super Member
because they're trying to port code from an existing application, not because they need or
want malloc() to solve their problem.
#8
magio
RE: Dynamic memory allocation Thursday, September 22, 2005 5:14 PM (permalink)
ORIGINAL: jtemples
0
All malloc does is give you a pointer to a chunk of memory of the specified
size. free() returns that chunk of memory to the pool so a subsequent call to
Super Member
malloc() can reuse it. You don't need an OS to do this. The reason it doesn't
show up in C compilers for small micros is that it's generally just not
practical to use. When people want it, it's usually because they're trying to
port code from an existing application, not because they need or want
malloc() to solve their problem.
http://www.microchip.com/forums/m114990.aspx
4/10
24/05/2016
#9
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 5:44 PM (permalink)
But the cost of implementing dynamic memory allocation (even by a OS kernel) goes
beyond that. The memory resource descriptor table takes space, and if you need to
manage memory block ownership you need a task owner descriptor in that table, and
maybe a task current resource list. When you have plenty of resources, this is relative
straightforward, but with low RAM, the cost can be unacceptable.
The garbage collector approach (like you described) requires another thing: the OS needs
to either translate pointers to actual addresses on-the-fly, by using relative indirection
with 'base address' visible by the OS, or the tasks need to have a callback to update the
pointers when the garbage collector moves blocks. This has an impact on performance as
well as on architecture.
There is a minimal approach to malloc, that can be implemented in an OS but does not
require it, that describes the memory pool as a bitmap in which the basic memory
allocation (int or long) is a bit in the bitmap. The malloc() and free() refer to the bitmap to
flag a allocated/free memory unit. This can be easily implemented in PIC18Fs.
Although for a multitasking system *alloc() functions are desirable, sometimes it make
sense just to analyse your code and use fixed allocation to preserve management
resources and increase performance.
The only scenario when I see the need for it is when you have tasks being
loaded/unloaded dynamically.
< Message edited by j_doin -- Sep. 22, 2005 9:15:51 AM >
#10
magio
RE: Dynamic memory allocation Thursday, September 22, 2005 5:59 PM (permalink)
And its true, is a design for tasks being loaded and unloaded, and I agree is not the
'common use' of a PIC.
Super Member
[8|]
http://www.microchip.com/forums/m114990.aspx
5/10
24/05/2016
#11
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 7:18 PM (permalink)
#12
whsiung
RE: Dynamic memory allocation Thursday, September 22, 2005 7:38 PM (permalink)
Hi,
Not sure if this's what you want: Dynamic Memory Allocation for the MPLAB C18 C
Compiler.
Super Member
Best regards,
Total Posts : 1461
Rew ard points : 0
WH Tan
Joined: 11/8/2003
Location: Malaysia
#13
Status: offline
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 7:48 PM (permalink)
http://www.microchip.com/forums/m114990.aspx
6/10
24/05/2016
Dynamically loadable/discardable tasks have another memory allocation issue: internal RAM
footprint of task variables and data objects. You have to handle variable space allocation
conflicts and inter-process shared data areas. You have also to guarantee the compiler
generates fully address-independent relocatable code, else you will have to patch opcodes
for longjumps and jumptables.
Is your application written for an external memory PIC? I'm preparing to port my
microkernel to a external memory architecture using the 18F8722. I have implemented it in
the iAPX'486 architecture with full *alloc() and task load support, including dynamic OS
memory compaction. But in a intel segmented protected mode architecture this is so much
simpler.
I would like to have task load/unload in the system (to allow external mass storage to
have 'PIC .EXE programs'), and task memory allocation is one critical issue.
But I have not decided yet if dynamic memory makes sense in the PIC architecture. I'm
tending to a static memory allocation, with the tasks being compiled as objects and having
a standard COFF file in the disk. The OS can have a 'linker snapshot' in memory and read
the COFF for the task being loaded in memory. Then the memory allocation needs simply
be extracted from the COFF and the loader 'patches' the relocatable objects in the binary
loading process at external RAM. The advantages are the simplicity of the programmer's
model, that remains essentially the same for internal RAM data allocation. If the 'runtime
linker' is ever implemented correctly, it would be funny to have a task internal variables
addresses being updated directly in the opcode fields on-the-fly by the OS. System global
and shared variables update would require a quite sofisticated linker, and certainly take
longer. Since almost always the system would be used for realtime response, this is not
trivial. So, a degree of internal memory allocation granularity is inevitable. This is all still in
the research phase, and I didn't look abroad at other PIC OSes to see how they handle
task loading/unloading.
The *alloc() functions in this external memory makes sense, and is very simple to
implement, and pointer access is natural for external memory RAM (which I believed will be
treated as rom by C18).
#14
magio
RE: Dynamic memory allocation Thursday, September 22, 2005 7:53 PM (permalink)
ORIGINAL: j_doin
0
That's interesting. And you are writing the OS kernel?
I'd like to hear your results / approaches to task management.
Super Member
No, im not writing an OS kernel. I just find the subject fascinating but now i just don't
Total Posts : 2164
http://www.microchip.com/forums/m114990.aspx
7/10
24/05/2016
#15
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 8:19 PM (permalink)
Ok. Maybe in the next 4 months I have something implemented, if I find time to spend on
this. When some concrete results come up, I may post at the forum.
#16
magio
RE: Dynamic memory allocation Thursday, September 22, 2005 8:46 PM (permalink)
May be you could recruit some 'volunteers' here to help you on the task.
Super Member
#17
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 9:41 PM (permalink)
#18
http://www.microchip.com/forums/m114990.aspx
8/10
24/05/2016
magio
RE: Dynamic memory allocation Thursday, September 22, 2005 10:57 PM (permalink)
Nice thing :)
Let us know about the project. Some of use could 'help' with some lines or at least with
#19
Guest
RE: Dynamic memory allocation Thursday, September 22, 2005 11:03 PM (permalink)
#20
Page:
>
Showing page 1 of 2
Home All Forums [Development Tools] MPLAB C18 Compiler Dynamic memory allocation
Jump to:
http://www.microchip.com/forums/m114990.aspx
9/10
24/05/2016
Latest Posts
Active Posts
All FAQs
Products Applications Design Support Training Sample & Buy About Us Contact Us Legal
Investors Careers
Copyright 1998-2014 Microchip Technology Inc. All rights reserved.
Shanghai ICP Recordal No.09049794
http://www.microchip.com/forums/m114990.aspx
10/10