Discussion:
Using OS_DynamicArea to roll my own memory allow & free routines.
(too old to reply)
u***@garethlock.com
2020-03-26 19:56:13 UTC
Permalink
Been trying to use malloc() and free() from C to create buffers for loading files... Once I've loaded and unsquashed one Sprite file, even if I return the file buffer (squashed data) back to the system, I seem unable to load more than one file before I overwrite the heap...

Is this something I'm doing wrong, or a limitation of malloc()? If it is indeed the second, can someone point me to an equivalent or some way of increasing this memory pool...
Matthew Phillips
2020-03-27 12:47:07 UTC
Permalink
Post by u***@garethlock.com
Been trying to use malloc() and free() from C to create buffers for loading
files... Once I've loaded and unsquashed one Sprite file, even if I return
the file buffer (squashed data) back to the system, I seem unable to load
more than one file before I overwrite the heap...
What environment is your program running in. Is it a Wimp application? Is
it a command-line program running in a TaskWindow, or what?

If you are running in a TaskWindow, you might need to increase the Next slot
in the Wimp *before* you open the TaskWindow.
Post by u***@garethlock.com
Is this something I'm doing wrong, or a limitation of malloc()?
Definitely something you are doing wrong!

By the way, your subject line is "Using OS_DynamicArea to roll my own memory
allow & free routines", but you don't seem to have said anything about this
in your posting. Is OS_DynamicArea relevant to your question?
--
Matthew Phillips
Durham
u***@garethlock.com
2020-03-27 14:05:51 UTC
Permalink
I'm running as a fullscreen single tasking app. What I'm trying to write is a C port of Brix. I've come to the conclusion that trying to implement all the features I have planned for this in BASIC is going to run way too slow. So I'm currently trying to re-implement this thing in C.

I have various test programs up and running, including timers, squash decompression, handling of TimPlayer from within C, reading mouse state etc. Indeed, I can get as far as displaying the loading screen with music. As soon as I proceed to load and decompress the other two Sprite files I come unstuck with "unrecoverable error in the run time system : Not enough memory, malloc failed, (heap overwritten)"

I've given myself a Next slot of 189952K, but this makes no difference at all...

I have three routines that make up the memory system...

void * mem_alloc(size_t s) {
size_t * ret = malloc(sizeof(size_t) + s);
*ret = s;
return &ret[1];
};

void mem_free(void * ptr) {
free((size_t*)ptr - 1);
};

size_t mem_size(void * ptr) {
return ((size_t*)ptr[-1];
};

This is to allow me to return the size of the allocated space.
Matthew Phillips
2020-03-27 17:34:04 UTC
Permalink
Post by u***@garethlock.com
As soon as I proceed to load and decompress the other two Sprite files I
come unstuck with "unrecoverable error in the run time system : Not enough
memory, malloc failed, (heap overwritten)"
I've given myself a Next slot of 189952K, but this makes no difference at all...
Most likely what is happening is that you are writing beyond the bounds of a
block of allocated memory and overwriting the pointers that are used by the
heap system to maintain its linked list of allocated memory.

This kind of error is very common if you make mistakes. It usually doesn't
mean that you have run out of memory, but that the heap has been corrupted.

A tool like Fortify can be compiled into your code to help you track down
where the fault is.

Matthew
--
Matthew Phillips
Durham
u***@garethlock.com
2020-03-27 19:30:19 UTC
Permalink
Ok... Where can I get a copy??
Matthew Phillips
2020-03-28 12:22:32 UTC
Permalink
Post by u***@garethlock.com
Ok... Where can I get a copy??
https://github.com/dpt/PrivateEye/blob/master/libs/fortify/FortifyDoc
--
Matthew Phillips
Durham
u***@garethlock.com
2020-03-28 15:20:23 UTC
Permalink
Ok... I've found it, but how the heck do I download it from there as a zip... I've tried to clone it from git on a pc and failed... Short of pulling each file individually...
u***@garethlock.com
2020-03-28 15:24:43 UTC
Permalink
Scratch that....
u***@garethlock.com
2020-04-04 23:52:12 UTC
Permalink
Ok... Got far enough today to confirm that mem_free() as defined in my second post to this topic is not freeing the allocation properly. Still trying to figure out what exactly is going on.
Loading...