6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 2:10 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Apr 12, 2017 9:34 am 
Offline
User avatar

Joined: Sun Dec 29, 2002 8:56 pm
Posts: 460
Location: Canada
I thought this might be a good topic to continue. http://forum.6502.org/viewtopic.php?f=12&t=4465&start=120#p52213
Changing times affects approaches to software.
For a "beginner" approach to memory management I'd suggest just statically allocating things.
Quote:
Why consume processing time in the firmware dynamically setting up fixed buffers that will be used in the exact same way every time? Doing such a thing makes no sense in the context of an eight bit system that has no memory management hardware and no expansion capabilities that could potentially alter the memory map in an arbitrary way.

I have to agree with this.
Quote:
If you're writing your own higher level language, such as Forth or BASIC, you'll need some form of dynamic memory to store the program, text strings, array variables, and such.

Tiny BASIC works quite well with statically allocated buffers.

Dynamic buffering can get to be quite complex software especially in a small system. If you've ever chased down what happen's for a malloc() for instance, including going through the OS code it's a big of a nightmare. It can burn up a lot of cycles affecting performance as well. For the system's I've setup using a 6502 like processor I use almost exclusively statically allocated buffers. That includes buffers for the tasks, stacks, video, I/O, etc. It is more bookwork to setup statically allocated buffers, but it sure makes the software simpler to code.

With the amounts of memory available today, it is possible to simply reserve a maximum amount that might be required for each task. (eg. 1k stack for 256 tasks is only 256k of memory). In many cases the maximum amounts required for tasks is not very much compared to memory capacities. Dynamic allocation became popular I think on system where memory limitations required minimal footprints and virtualization of memory.

_________________
http://www.finitron.ca


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 12, 2017 9:49 am 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
Rob Finch wrote:
Tiny BASIC works quite well with statically allocated buffers.

Tiny BASIC doesn't have strings or arrays, so it can be argued that it doesn't "work well".

Quote:
Dynamic buffering can get to be quite complex software especially in a small system.

In most of my embedded projects, I have my own malloc/free code, in about 200 lines of C. Complexity is fairly low, it keeps all free blocks in a linked list, and does a best fit algorithm to pick a block. I use it mostly for allocating network buffers. Using dynamic memory allows the best use of limited memory.


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 12, 2017 5:39 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8514
Location: Midwestern USA
Rob Finch wrote:
Arlet wrote:
If you're writing your own higher level language, such as Forth or BASIC, you'll need some form of dynamic memory to store the program, text strings, array variables, and such.
Tiny BASIC works quite well with statically allocated buffers.

However, a realistic BASIC interpreter has to be able to dynamically allocate and deallocate memory as variables are created, modified and destroyed. Garbage collection also gets into the picture in the case of string management, which in itself could be complicated.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Last edited by BigDumbDinosaur on Thu Apr 13, 2017 3:15 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 12, 2017 8:42 pm 
Offline
User avatar

Joined: Sun Dec 29, 2002 8:56 pm
Posts: 460
Location: Canada
It occurs to me not to be confused in the definitions between dynamic memory allocation and heap based allocations. Dynamic memory allocations are typically heap based. Dynamic as in at run time could also mean allocated from static storage.

I have found static allocation to be extremely useful when trying to bootstrap a processor and OS. The problem with dynamic (heap)memory allocation is that so much of the system has to be already working in order for it to work.
Quote:
I have my own malloc/free code, in about 200 lines of C.

Sure this doesnt' seem like much, but it's still greater than none. That C code would typically have to be written in assembler for the '02. For a small app not having to worry about dynamically allocated stuff can be a boon.

Most data with the exception of blobs have fixed sizes. Fixed sized data records can be defined in most cases.
Dynamically allocated memory does have its uses just like recursion can be a useful tool sometimes. But it gets overused sometimes and can impact performance. In the typical 6502 app it seems to me that statically allocating memory might be preferred.

Whether memory is allocated dynamically (on heap) or not, can be hidden by the function call to allocate memory, so that the app program doesn't actually have to know.
An object pointer is returned from an object factory. How that factory allocates memory for the object can be up to the factory.

Quote:
However, a realistic BASIC interpreter has to be able to dynamically allocate and deallocate memory as variables are created, modified and destroyed. Garbage collection also gets into the picture in the case of string management, which in itself could be complicated.

Dynamic as in at run time - true, but not necessarily heap based.
Allocating and deallocating memory for variables does not necessarily have to be done with dynamic (heap) allocations. A statically allocated pool of variables available to be assigned works quite well. One megabyte could be reserved to hold a lot of 32 bit integer variables with names and handles for implementing lists. Note that having larger amounts of memory available makes this practical. One approach to strings would be to use 256 byte max length string vars. 1000 strings * 256 bytes is only 256k of memory. However, If the system is limited to 8kB of memory then some form of dynamic sizing of variable areas is probably required.

_________________
http://www.finitron.ca


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 12, 2017 9:18 pm 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
Quote:
Sure this doesnt' seem like much, but it's still greater than none.

Most microcontrollers have much more flash memory than SRAM. A bit of extra flash code to make better use of SRAM is often worth it. On a system I'm working on right now, I have 1 MB of flash, 128 kB of SRAM, and the malloc/free code takes 400 bytes. I think that's a worthwhile effort.
Quote:
The problem with dynamic (heap)memory allocation is that so much of the system has to be already working in order for it to work.

Well, you could leave out free(), and replace malloc() with some simple code to return old heap pointer, while updating pointer with size of object. That only takes a handful of instructions, even on a 6502. When you've got the rest of the system working, you can come back and improve it.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: