size, align, region);
if (region & 2)
- chunk = _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, align);
+ chunk = _malloc(&ZoneLow, size, align);
if (!chunk && (region & 1))
- chunk = _malloc(&ZoneFSeg, MALLOC_DEFAULT_HANDLE, size, align);
+ chunk = _malloc(&ZoneFSeg, size, align);
dprintf(3, "Legacy16GetTableAddress size %x align %x region %d yields %p\n",
size, align, region, chunk);
file->file = romfile_find(entry->alloc_file);
if (!file->file || !file->file->size)
return;
- data = _malloc(zone, MALLOC_DEFAULT_HANDLE, file->file->size, alloc_align);
+ data = _malloc(zone, file->file->size, alloc_align);
if (!data) {
warn_noalloc();
return;
// Allocate memory from the given zone and track it as a PMM allocation
void * __malloc
-_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
+_malloc(struct zone_s *zone, u32 size, u32 align)
{
ASSERT32FLAT();
if (!size)
if (!detail)
return NULL;
}
+ detail->handle = MALLOC_DEFAULT_HANDLE;
// Find and reserve space for main allocation
void *data = allocSpace(zone, size, align, &detail->datainfo);
return NULL;
}
- dprintf(8, "_malloc zone=%p handle=%x size=%d align=%x ret=%p (detail=%p)\n"
- , zone, handle, size, align, data, detail);
- detail->handle = handle;
+ dprintf(8, "_malloc zone=%p size=%d align=%x ret=%p (detail=%p)\n"
+ , zone, size, align, data, detail);
return data;
}
return maxspace - reserve;
}
+// Set a handle associated with an allocation.
+void
+malloc_sethandle(void *data, u32 handle)
+{
+ ASSERT32FLAT();
+ struct allocinfo_s *info = findAlloc(data);
+ if (!info || data == (void*)info || data == info->dataend)
+ return;
+ struct allocdetail_s *detail = container_of(
+ info, struct allocdetail_s, datainfo);
+ detail->handle = handle;
+}
+
// Find the data block allocated with _malloc with a given handle.
void *
-malloc_find(u32 handle)
+malloc_findhandle(u32 handle)
{
int i;
for (i=0; i<ARRAY_SIZE(Zones); i++) {
extern u32 LegacyRamSize;
void malloc_init(void);
void malloc_prepboot(void);
-void *_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
+void *_malloc(struct zone_s *zone, u32 size, u32 align);
int _free(void *data);
u32 malloc_getspace(struct zone_s *zone);
-void *malloc_find(u32 handle);
+void malloc_sethandle(void *data, u32 handle);
+void *malloc_findhandle(u32 handle);
#define MALLOC_DEFAULT_HANDLE 0xFFFFFFFF
// Minimum alignment of malloc'd memory
#define MALLOC_MIN_ALIGN 16
// Helper functions for memory allocation.
static inline void *malloc_low(u32 size) {
- return _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+ return _malloc(&ZoneLow, size, MALLOC_MIN_ALIGN);
}
static inline void *malloc_high(u32 size) {
- return _malloc(&ZoneHigh, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+ return _malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
}
static inline void *malloc_fseg(u32 size) {
- return _malloc(&ZoneFSeg, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+ return _malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
}
static inline void *malloc_tmplow(u32 size) {
- return _malloc(&ZoneTmpLow, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+ return _malloc(&ZoneTmpLow, size, MALLOC_MIN_ALIGN);
}
static inline void *malloc_tmphigh(u32 size) {
- return _malloc(&ZoneTmpHigh, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+ return _malloc(&ZoneTmpHigh, size, MALLOC_MIN_ALIGN);
}
static inline void *malloc_tmp(u32 size) {
void *ret = malloc_tmphigh(size);
return malloc_tmplow(size);
}
static inline void *memalign_low(u32 align, u32 size) {
- return _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, align);
+ return _malloc(&ZoneLow, size, align);
}
static inline void *memalign_high(u32 align, u32 size) {
- return _malloc(&ZoneHigh, MALLOC_DEFAULT_HANDLE, size, align);
+ return _malloc(&ZoneHigh, size, align);
}
static inline void *memalign_tmplow(u32 align, u32 size) {
- return _malloc(&ZoneTmpLow, MALLOC_DEFAULT_HANDLE, size, align);
+ return _malloc(&ZoneTmpLow, size, align);
}
static inline void *memalign_tmphigh(u32 align, u32 size) {
- return _malloc(&ZoneTmpHigh, MALLOC_DEFAULT_HANDLE, size, align);
+ return _malloc(&ZoneTmpHigh, size, align);
}
static inline void *memalign_tmp(u32 align, u32 size) {
void *ret = memalign_tmphigh(align, size);
if (align < MALLOC_MIN_ALIGN)
align = MALLOC_MIN_ALIGN;
}
+ void *data;
switch (flags & 3) {
default:
case 0:
return 0;
case 1:
- return (u32)_malloc(lowzone, handle, size, align);
+ data = _malloc(lowzone, size, align);
+ break;
case 2:
- return (u32)_malloc(highzone, handle, size, align);
+ data = _malloc(highzone, size, align);
+ break;
case 3: {
- void *data = _malloc(lowzone, handle, size, align);
- if (data)
- return (u32)data;
- return (u32)_malloc(highzone, handle, size, align);
+ data = _malloc(lowzone, size, align);
+ if (!data)
+ data = _malloc(highzone, size, align);
}
}
+ if (data && handle != MALLOC_DEFAULT_HANDLE)
+ malloc_sethandle(data, handle);
+ return (u32)data;
}
// PMM - find
dprintf(3, "pmm01: handle=%x\n", handle);
if (handle == MALLOC_DEFAULT_HANDLE)
return 0;
- return (u32)malloc_find(handle);
+ return (u32)malloc_findhandle(handle);
}
// PMM - deallocate