/* KallistiOS ##version## pvr_mem.c Copyright (C) 2002 Megan Potter */ #include #include #include #include /* For the struct mallinfo defs */ #include #include #include /* This module basically serves as a KOS-friendly front end and support routines for the pvr_mem_core module, which is a dlmalloc-derived malloc for use with the PVR memory pool. I was originally going to make a totally separate thing that could be used to generically manage any memory pool, but then I realized what a gruelling and thankless task that would be when starting with dlmalloc, so we have this instead. ^_^; */ /* Bring in some prototypes from pvr_mem_core.c */ /* We can't directly include its header because of name clashes with the real malloc header */ extern void * pvr_int_malloc(size_t bytes); extern void pvr_int_free(void *ptr); extern struct mallinfo pvr_int_mallinfo(void); extern void pvr_int_mem_reset(void); extern void pvr_int_malloc_stats(void); #include #include /* List of allocated memory blocks for leak checking */ typedef struct memctl { uint32_t size; tid_t thread; uint32_t addr; pvr_ptr_t block; LIST_ENTRY(memctl) list; } memctl_t; static LIST_HEAD(memctl_list, memctl) block_list; /* PVR RAM base; NULL is considered invalid */ static pvr_ptr_t pvr_mem_base = NULL; static pvr_ptr_t pvr_mem_sbrk = NULL; #define CHECK_MEM_SBRK \ assert_msg(pvr_mem_sbrk != NULL, \ "pvr_mem_* used, but PVR hasn't been initialized yet") /* Used in pvr_mem_core.c */ void *pvr_int_sbrk(size_t amt) { uint32_t old, n; /* Are we valid? */ CHECK_MEM_SBRK; /* Try to increment it */ old = (uint32_t)pvr_mem_sbrk; n = old + amt; /* Did we run over? */ if(n > PVR_RAM_INT_TOP) return (void *) - 1; /* Nope, everything's cool */ pvr_mem_sbrk = (pvr_ptr_t)n; return (pvr_ptr_t)old; } /* Allocate a chunk of memory from texture space; the returned value will be relative to the base of texture memory (zero-based) */ pvr_ptr_t __weak_symbol pvr_mem_malloc(size_t size) { uint32_t rv32; memctl_t *ctl; CHECK_MEM_SBRK; rv32 = (uint32_t)pvr_int_malloc(size); assert_msg((rv32 & 0x1f) == 0, "dlmalloc's alignment is broken; " "please make a bug report"); if(__is_defined(PVR_KM_DBG)) { ctl = malloc(sizeof(memctl_t)); if(!ctl) return (pvr_ptr_t)rv32; ctl->size = size; ctl->thread = thd_current->tid; ctl->addr = arch_get_ret_addr(); ctl->block = (pvr_ptr_t)rv32; LIST_INSERT_HEAD(&block_list, ctl, list); } if(__is_defined(PVR_KM_DBG_VERBOSE)) { printf("Thread %d/%08lx allocated %lu bytes at %08lx\n", ctl->thread, ctl->addr, ctl->size, rv32); } return (pvr_ptr_t)rv32; } /* Free a previously allocated chunk of memory */ void __weak_symbol pvr_mem_free(pvr_ptr_t chunk) { uint32_t ra; memctl_t *ctl, *tmp; int found; if(__is_defined(PVR_KM_DBG)) ra = arch_get_ret_addr(); CHECK_MEM_SBRK; if(__is_defined(PVR_KM_DBG_VERBOSE)) { printf("Thread %d/%08lx freeing block @ %08lx\n", thd_current->tid, ra, (uint32_t)chunk); } if(__is_defined(PVR_KM_DBG)) { found = 0; LIST_FOREACH_SAFE(ctl, &block_list, list, tmp) { if(ctl->block == chunk) { LIST_REMOVE(ctl, list); free(ctl); found = 1; break; } } if(!found) { dbglog(DBG_ERROR, "pvr_mem_free: trying to free non-alloc'd block " "%08lx (called from %d/%08lx\n", (uint32_t)chunk, thd_current->tid, ra); } } pvr_int_free((void *)chunk); } /* Check the memory block list to see what's allocated */ void __weak_symbol pvr_mem_print_list(void) { memctl_t *ctl; if(!__is_defined(PVR_KM_DBG)) return; printf("pvr_mem_print_list block list:\n"); LIST_FOREACH(ctl, &block_list, list) { printf(" unfreed block at %08lx size %lu, " "allocated by thread %d/%08lx\n", (unsigned long)ctl->block, ctl->size, ctl->thread, (unsigned long)ctl->addr); } printf("pvr_mem_print_list end block list\n"); } /* Return the number of bytes available still in the memory pool */ static size_t pvr_mem_available_int(void) { struct mallinfo mi = pvr_int_mallinfo(); /* This magic formula is modeled after mstats() */ return mi.arena - mi.uordblks; } size_t __weak_symbol pvr_mem_available(void) { if(!pvr_mem_sbrk) return 0; return pvr_mem_available_int() + (PVR_RAM_INT_TOP - (size_t)pvr_mem_sbrk); } /* Reset the memory pool, equivalent to freeing all textures currently residing in RAM. This _must_ be done on a mode change, configuration change, etc. */ void __weak_symbol pvr_mem_reset(void) { if (pvr_mem_base != NULL) { pvr_mem_sbrk = pvr_mem_base; pvr_int_mem_reset(); } else { pvr_mem_sbrk = NULL; } } void __weak_symbol pvr_mem_initialize(pvr_ptr_t pvr_texture_base, size_t available_memory) { (void)available_memory; pvr_mem_base = pvr_texture_base; } /* Print some statistics (like mallocstats) */ void __weak_symbol pvr_mem_stats(void) { printf("pvr_mem_stats():\n"); pvr_int_malloc_stats(); printf("max sbrk base: %08lx\n", (uint32_t)pvr_mem_sbrk); pvr_mem_print_list(); }