FryingPan/DTLib/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/DTLib/mmakefile.src b/FryingPan/DTLib/mmakefile.src index 5fa40a221..70dd1ab3c 100644 --- a/FryingPan/DTLib/mmakefile.src +++ b/FryingPan/DTLib/mmakefile.src @@ -44,4 +44,4 @@ USER_CXXFLAGS += $(CFLAGS_NO_RTTI) $(CFLAGS_NO_EXCEPTIONS) -noposixc USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -L$(GENDIR)/$(CURDIR)/../framework -Wl,--start-group -lGeneric -lLibrary -lC -Wl,--end-group %build_prog mmake=contrib-fryingpan-dtlib progname="dtlibio.module" \ - targetdir=$(DESTDIR) files= cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit" usestartup=no + targetdir=$(DESTDIR) files= cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit arossupport" usestartup=no FryingPan/Expat/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/Expat/mmakefile.src b/FryingPan/Expat/mmakefile.src index 1a9d11380..41cfc3273 100644 --- a/FryingPan/Expat/mmakefile.src +++ b/FryingPan/Expat/mmakefile.src @@ -32,4 +32,4 @@ USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -L$(GENDI %build_prog mmake=contrib-fryingpan-expat \ progname="expat2.library" targetdir=$(DESTDIR) files="$(FILES)" \ - cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit" usestartup=no + cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit arossupport" usestartup=no FryingPan/Expat/xmlparse.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/FryingPan/Expat/xmlparse.c b/FryingPan/Expat/xmlparse.c index 46d5afb02..d8a10404b 100644 --- a/FryingPan/Expat/xmlparse.c +++ b/FryingPan/Expat/xmlparse.c @@ -643,6 +643,37 @@ struct XML_ParserStruct { #define paramEntityParsing (parser->m_paramEntityParsing) #endif /* XML_DTD */ +static void *mymalloc(size_t allocsize) { + void *allocaddr; + //ADB( + kprintf("[fryingpan:expat] %s(%u)\n", __func__, allocsize); + //) + allocaddr = malloc(allocsize); + //ADB( + kprintf("[fryingpan:expat] %s: returning 0x%p\n", __func__, allocaddr); + //) + return allocaddr; +} + +static void *myrealloc(void *addr, size_t len) { + void *allocaddr; + //ADB( + kprintf("[fryingpan:expat] %s(0x%p, len)\n", __func__, addr); + //) + allocaddr = realloc(addr, len); + //ADB( + kprintf("[fryingpan:expat] %s: returning 0x%p\n", __func__, allocaddr); + //) + return allocaddr; +} + +static void myfree(void *addr) { + //ADB( + kprintf("[fryingpan:expat] %s(0x%p)\n", __func__, addr); + //) + free(addr); +} + XML_Parser XMLCALL XML_ParserCreate(const XML_Char *encodingName) { @@ -706,9 +737,9 @@ parserCreate(const XML_Char *encodingName, parser = (XML_Parser)malloc(sizeof(struct XML_ParserStruct)); if (parser != 0) { mtemp = (XML_Memory_Handling_Suite *)&(parser->m_mem); - mtemp->malloc_fcn = malloc; - mtemp->realloc_fcn = realloc; - mtemp->free_fcn = free; + mtemp->malloc_fcn = mymalloc; + mtemp->realloc_fcn = myrealloc; + mtemp->free_fcn = myfree; } } FryingPan/FP/Engine/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/FP/Engine/mmakefile.src b/FryingPan/FP/Engine/mmakefile.src index 7774091cc..e02c22a02 100644 --- a/FryingPan/FP/Engine/mmakefile.src +++ b/FryingPan/FP/Engine/mmakefile.src @@ -45,4 +45,4 @@ USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -L$(GENDI %build_prog mmake=contrib-fryingpan-fp-engine \ progname="fryingpan.engine" targetdir=$(DESTDIR) files= \ - cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit" usestartup=no + cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit arossupport" usestartup=no FryingPan/FP/GUI-MUI/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/FP/GUI-MUI/mmakefile.src b/FryingPan/FP/GUI-MUI/mmakefile.src index 785219426..886897344 100644 --- a/FryingPan/FP/GUI-MUI/mmakefile.src +++ b/FryingPan/FP/GUI-MUI/mmakefile.src @@ -61,4 +61,4 @@ USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -Wl,--sta %build_prog mmake=contrib-fryingpan-fp-guimui \ progname="mui.interface" targetdir=$(DESTDIR) files= cxxfiles="$(CXXFILES)" \ - uselibs="amiga libinit autoinit" usestartup=no lto=no + uselibs="amiga libinit autoinit arossupport" usestartup=no lto=no FryingPan/FP/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/FP/mmakefile.src b/FryingPan/FP/mmakefile.src index 5719545d6..ec7c3b936 100644 --- a/FryingPan/FP/mmakefile.src +++ b/FryingPan/FP/mmakefile.src @@ -28,4 +28,4 @@ USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -L$(GENDI #USER_CPPFLAGS := -DDEBUG %build_prog mmake=contrib-fryingpan-fp progname="FryingPan" \ - targetdir=$(DESTDIR) files="" cxxfiles="$(FILES)" uselibs="amiga" usestartup=no + targetdir=$(DESTDIR) files="" cxxfiles="$(FILES)" uselibs="amiga arossupport" usestartup=no FryingPan/ISOBuilder/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/ISOBuilder/mmakefile.src b/FryingPan/ISOBuilder/mmakefile.src index 3f8efe6d2..dfb9dc592 100644 --- a/FryingPan/ISOBuilder/mmakefile.src +++ b/FryingPan/ISOBuilder/mmakefile.src @@ -43,4 +43,4 @@ USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -L$(GENDI %build_prog mmake=contrib-fryingpan-isobuilder \ progname="isobuilder.module" targetdir=$(DESTDIR) files= \ - cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit" usestartup=no + cxxfiles="$(CXXFILES)" uselibs="amiga libinit autoinit arossupport" usestartup=no FryingPan/Optical/Drive.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/FryingPan/Optical/Drive.cpp b/FryingPan/Optical/Drive.cpp index 84c6e26e4..3bbe4d5f9 100644 --- a/FryingPan/Optical/Drive.cpp +++ b/FryingPan/Optical/Drive.cpp @@ -32,10 +32,6 @@ #include "Humming.h" #include - #ifndef COMPILER_BARRIER -# define COMPILER_BARRIER() __asm__ __volatile__ ("" ::: "memory") -#endif - DriveSpool *DriveSpool::pInstance = 0; Drive *Drive::OpenDrive(Drive *& pChain, char* pDevice, int lUnit) @@ -66,7 +62,6 @@ Drive::Drive(char *drv, int unit) _ND("Drive"); next_drive = 0; - COMPILER_BARRIER(); clients = 0; current_disc.Assign(0); bLockInterOperations = false; @@ -130,26 +125,16 @@ void Drive::RemClient(DriveClient* el) bool Drive::ProcInit() { driveio = 0; - COMPILER_BARRIER(); mode_io = 0; // if anything fails - COMPILER_BARRIER(); - inquiry = 0; - COMPILER_BARRIER(); tur = 0; // we don't dispose illegal pointers. - COMPILER_BARRIER(); features = 0; - COMPILER_BARRIER(); - discinfo = 0; - COMPILER_BARRIER(); - hwconfig = 0; - COMPILER_BARRIER(); + inquiry = 0; pc = 0; - COMPILER_BARRIER(); pw = 0; - - selected_read_speed = 0xFFFF; - COMPILER_BARRIER(); - selected_write_speed = 0xFFFF; + discinfo = 0; + hwconfig = 0; + selected_read_speed = 0xffff; + selected_write_speed = 0xffff; current_disc.Assign(0); driveio = new DriveIO(DEBUG_ENGINE); FryingPan/Optical/mmakefile.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FryingPan/Optical/mmakefile.src b/FryingPan/Optical/mmakefile.src index 8c0fbf78d..4883e02ef 100644 --- a/FryingPan/Optical/mmakefile.src +++ b/FryingPan/Optical/mmakefile.src @@ -60,4 +60,4 @@ USER_LDFLAGS := $(TARGET_CXX_LDFLAGS) -nostdlib $(CXXFLAGS_NOSTDLIBXX) -L$(GENDI %build_prog mmake=contrib-fryingpan-optical progname="Optical" \ targetdir=$(DESTDIR) files= cxxfiles="$(CXXFILES)" \ - uselibs="amiga libinit autoinit" usestartup=no + uselibs="amiga libinit autoinit arossupport" usestartup=no FryingPan/framework/Generic/XMLDocument.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/FryingPan/framework/Generic/XMLDocument.cpp b/FryingPan/framework/Generic/XMLDocument.cpp index 97bb77b5c..39588d68d 100644 --- a/FryingPan/framework/Generic/XMLDocument.cpp +++ b/FryingPan/framework/Generic/XMLDocument.cpp @@ -391,9 +391,10 @@ bool XMLDocument::ReadXML(const char *sName) hParse.Empty(); pExpat->XML_SetUserData(pParser, this); +#if (0) pExpat->XML_SetElementHandler(pParser, &XMLDocument::fXMLStartHandler, &XMLDocument::fXMLEndHandler); pExpat->XML_SetCharacterDataHandler(pParser, &XMLDocument::fXMLContentHandler); - + do { len = 0; @@ -410,7 +411,8 @@ bool XMLDocument::ReadXML(const char *sName) } } while (0 != len); - +#endif + DOS->Close(fh); pExpat->XML_ParserFree(pParser); pParser = 0; FryingPan/framework/LibC/LibC.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/FryingPan/framework/LibC/LibC.h b/FryingPan/framework/LibC/LibC.h index 2a463bbd4..791c35f72 100644 --- a/FryingPan/framework/LibC/LibC.h +++ b/FryingPan/framework/LibC/LibC.h @@ -42,10 +42,26 @@ extern struct Library *__InternalIntuitionBase; extern struct Library *__InternalUtilityBase; extern struct WBStartup *__WBStartup; extern uint32 StartupFlags; + +#ifdef FP_AROSDEBUG +void kprintf(const char *formatString,...); +#define ADB(x) x +#else +void kprintf(const char *formatString,...); +#define ADB(x) +#endif + #ifdef __cplusplus } #endif +struct FPMemPrivate { + APTR allocpool; + ULONG allocwanted; + ULONG allocsize; + APTR allocraw; +}; + enum StartupFlag { SF_NoWBStartup = 1 FryingPan/framework/LibC/delete.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/FryingPan/framework/LibC/delete.cpp b/FryingPan/framework/LibC/delete.cpp index 210a8419f..a23bf132b 100644 --- a/FryingPan/framework/LibC/delete.cpp +++ b/FryingPan/framework/LibC/delete.cpp @@ -20,20 +20,33 @@ #include "LibC.h" void operator delete(void* mem) { - Allocator** d = (Allocator**)mem; - --d; - (*d)->free(*d, d); + void *allocadr; + ADB(kprintf("[fryingpan] %s(0x%p)\n", __func__, mem)); + + Allocator** d = reinterpret_cast(mem); + --d; +#if defined(AROS_WORSTALIGN) + void** tmpmem = reinterpret_cast(reinterpret_cast(mem) - sizeof(void *)); + allocadr = tmpmem[-1]; +#else + allocadr = reinterpret_cast(d); +#endif + ADB(kprintf("[fryingpan] %s: allocator @ 0x%p, freeing 0x%p\n", __func__, d, allocadr)); + (*d)->free(*d, allocadr); } void operator delete[](void* mem) { - operator delete(mem); + ADB(kprintf("[fryingpan] %s(0x%p)\n", __func__, mem)); + operator delete(mem); } void operator delete(void* mem, std::size_t sz) { - operator delete(mem); + ADB(kprintf("[fryingpan] %s(0x%p, %ubytes)\n", __func__, mem, sz)); + operator delete(mem); } -void operator delete(void* ptr, std::align_val_t) { +void operator delete(void* ptr, std::align_val_t align) { + ADB(kprintf("[fryingpan] %s(0x%p, %u)\n", __func__, ptr, align)); if (ptr) { void* raw = reinterpret_cast(ptr)[-1]; Allocator* a = reinterpret_cast(raw)[0]; // allocator is stored at raw @@ -42,5 +55,6 @@ void operator delete(void* ptr, std::align_val_t) { } void operator delete[](void* ptr, std::align_val_t align) { + ADB(kprintf("[fryingpan] %s(0x%p, %u)\n", __func__, ptr, align)); operator delete(ptr, align); } FryingPan/framework/LibC/free.c | 95 ++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/FryingPan/framework/LibC/free.c b/FryingPan/framework/LibC/free.c index 6f58fba82..377fb2b15 100644 --- a/FryingPan/framework/LibC/free.c +++ b/FryingPan/framework/LibC/free.c @@ -24,57 +24,72 @@ void free(void* pMem) { - void* pPool; - long lSize; + struct FPMemPrivate *priv; + void* pPool; + size_t lSize; /* use size_t to match stored allocsize */ - if (NULL == pMem) - return; + if (NULL == pMem) + return; + + //ADB( + kprintf("[fryingpan] %s: allocation @ 0x%p\n", __func__, pMem); + //) + +#ifndef MEMDEBUG + /* subtract bytes from the byte-address, then cast to the struct pointer */ + priv = (struct FPMemPrivate *)((IPTR)pMem - (IPTR)sizeof(struct FPMemPrivate)); + pPool = priv->allocpool; + lSize = priv->allocsize; + pMem = priv->allocraw; + + //ADB( + kprintf("[fryingpan] %s: pool @ 0x%p, raw = 0x%p (%u bytes)\n", __func__, pPool, pMem, lSize); + //) -#ifndef DEBUG - pMem = &((void**)pMem)[-2]; - pPool = ((void**)pMem)[0]; - lSize = ((IPTR *)pMem)[1]; #else - pMem = &((void**)pMem)[-(2+(128>>3))]; - pPool = ((void**)pMem)[0]; - lSize = ((IPTR *)pMem)[1]; + IPTR *pMem1 = (IPTR *)pMem; + /* indices computed from MEMDEBUG_CANARY_SLOTS to avoid magic numbers */ + const int META_POOL_INDEX = -(MEMDEBUG_CANARY_SLOTS + 1); /* -17 */ + const int META_SIZE_INDEX = -(MEMDEBUG_CANARY_SLOTS + 2); /* -18 */ + const int META_RAW_INDEX = -(MEMDEBUG_CANARY_SLOTS + 3); /* -19 */ - { - IPTR *pMem1 = &((IPTR *)pMem)[2+(128>>3)]; - IPTR *pMem2 = &((IPTR *)pMem)[lSize>>2]; - uint32 i; + pPool = ((void**)pMem)[META_POOL_INDEX]; + lSize = ((IPTR *)pMem)[META_SIZE_INDEX]; + pMem = (void*)((IPTR *)pMem)[META_RAW_INDEX]; - for (i=1; i<=(128>>3); i++) - { - if ((pMem1[-i] != 0xC0DEDBAD) || - (pMem2[-i] != 0xC0DEDBAD)) - { - _error("Freeing corrupted memory block!\nSize: %ld bytes\nMEMORY WILL NOT BE FREED!\nPLEASE REPORT!", lSize-128-8); - return; - } - } - } -#endif + /* verify canaries */ + { + IPTR *pMem2 = (IPTR *)((IPTR)pMem + (IPTR)lSize); + uint32 i; + for (i = 1; i <= (uint32)MEMDEBUG_CANARY_SLOTS; ++i) + { + if ((pMem1[-(int)i] != (IPTR)0xC0DEDBAD) || + (pMem2[-(int)i] != (IPTR)0xC0DEDBAD)) + { + _error("Freeing corrupted memory block!\nSize: %zu bytes\nMEMORY WILL NOT BE FREED!\nPLEASE REPORT!", lSize - (MEMDEBUG_CANARY_SLOTS * sizeof(IPTR)) - (IPTR)sizeof(void *)); + return; + } + } + } +#endif - if (0 != pPool) - { + /* Now free using either the block's pool or the internal pool */ + if (NULL != pPool) { #ifndef __amigaos4__ - FreePooled(pPool, pMem, lSize); + FreePooled(pPool, pMem, (IPTR)lSize); #else - IExec->FreePooled(pPool, pMem, lSize); + IExec->FreePooled(pPool, pMem, (IPTR)lSize); #endif - } - else - { + } else { #ifndef __amigaos4__ - ObtainSemaphore(&__InternalSemaphore); - FreePooled(__InternalMemPool, pMem, lSize); - ReleaseSemaphore(&__InternalSemaphore); + ObtainSemaphore(&__InternalSemaphore); + FreePooled(__InternalMemPool, pMem, (IPTR)lSize); + ReleaseSemaphore(&__InternalSemaphore); #else - IExec->ObtainSemaphore(&__InternalSemaphore); - IExec->FreePooled(__InternalMemPool, pMem, lSize); - IExec->ReleaseSemaphore(&__InternalSemaphore); + IExec->ObtainSemaphore(&__InternalSemaphore); + IExec->FreePooled(__InternalMemPool, pMem, (IPTR)lSize); + IExec->ReleaseSemaphore(&__InternalSemaphore); #endif - } + } } FryingPan/framework/LibC/malloc.c | 282 ++++++++++++++++++++++++++------------ 1 file changed, 195 insertions(+), 87 deletions(-) diff --git a/FryingPan/framework/LibC/malloc.c b/FryingPan/framework/LibC/malloc.c index f0ad14b57..2fa6567f7 100644 --- a/FryingPan/framework/LibC/malloc.c +++ b/FryingPan/framework/LibC/malloc.c @@ -28,124 +28,232 @@ // free and realloc! // you may want to change these if you modify anything here. +#if defined(AROS_WORSTALIGN) +#define MALLOC_ALIGN AROS_WORSTALIGN +#else +#define MALLOC_ALIGN 8 +#endif + void* malloc(size_t lSize) { - IPTR *pMem = 0; + struct FPMemPrivate *priv = NULL; + void *raw = NULL; + void *pMem = NULL; + size_t mallocSize = 0; + size_t prefix_bytes = 0; - if (0 == lSize) - return 0; + if (lSize == 0) + return NULL; - lSize = ((lSize + 7) & ~7) + (sizeof(IPTR) << 1); + ADB(kprintf("[fryingpan] %s: requested size = %zu\n", __func__, lSize)); - if (0 != __InternalMemPool) - { -#ifndef DEBUG + if (__InternalMemPool == NULL) + return NULL; + + ADB(kprintf("[fryingpan] %s: internal pool @ %p\n", __func__, __InternalMemPool)); + +#ifndef MEMDEBUG + /* Non-debug path: prefix is size of FPMemPrivate */ + prefix_bytes = sizeof(struct FPMemPrivate); + + /* Overflow detection */ + if (lSize + prefix_bytes < lSize) { + return NULL; /* overflow */ + } + if (lSize + prefix_bytes + (MALLOC_ALIGN - 1) < lSize + prefix_bytes) { + return NULL; /* overflow */ + } + + mallocSize = (lSize + prefix_bytes + (MALLOC_ALIGN - 1)) & ~(IPTR)(MALLOC_ALIGN - 1); + + ADB(kprintf("[fryingpan] %s: adjusted size = %zu\n", __func__, mallocSize)); + + for (;;) { #ifndef __amigaos4__ - ObtainSemaphore(&__InternalSemaphore); // we wait only for our tasks! - pMem = (IPTR*)AllocPooled(__InternalMemPool, lSize); // Get memory - ReleaseSemaphore(&__InternalSemaphore); // and free sem. + ObtainSemaphore(&__InternalSemaphore); + raw = (void*)AllocPooled(__InternalMemPool, mallocSize); + ReleaseSemaphore(&__InternalSemaphore); #else - IExec->ObtainSemaphore(&__InternalSemaphore); // we wait only for our tasks! - pMem = (IPTR*)IExec->AllocPooled(__InternalMemPool, lSize); // Get memory - IExec->ReleaseSemaphore(&__InternalSemaphore); // and free sem. + IExec->ObtainSemaphore(&__InternalSemaphore); + raw = (void*)IExec->AllocPooled(__InternalMemPool, mallocSize); + IExec->ReleaseSemaphore(&__InternalSemaphore); #endif + if (raw == NULL) { + /* ask user to retry; if they cancel, give up */ + _error("Unable to allocate %zu bytes of memory!!!\nClick 'OK' to retry", lSize); + /* else loop and try again */ + continue; + } + + /* success: compute aligned returned pointer (byte arithmetic) */ + { + IPTR base = (IPTR)raw; + IPTR addr = base + prefix_bytes; /* leave room for priv */ + addr = (addr + (MALLOC_ALIGN - 1)) & ~(IPTR)(MALLOC_ALIGN - 1); + + priv = (struct FPMemPrivate*)(addr - sizeof(struct FPMemPrivate)); + pMem = (void*)addr; + + ADB(kprintf("[fryingpan] %s: alloc @ %p (raw %p)\n", __func__, pMem, raw)); + + priv->allocpool = NULL; + priv->allocwanted = lSize; + priv->allocsize = mallocSize; + priv->allocraw = raw; + } + + break; /* done */ + } - if (0 != pMem) - { - pMem[0] = 0; // set mem pool - pMem[1] = lSize; // set mem size - pMem = &pMem[2]; // set mem handle - } #else - uint32 i; - IPTR *pMem2 = 0; - lSize += (sizeof(IPTR) << 4); + /* MEMDEBUG path: reserve space for canaries + metadata before returned pointer */ + prefix_bytes = MEMDEBUG_PREFIX_SLOTS * sizeof(IPTR); +#if !defined(__AROS__) || __WORDSIZE==32 + prefix_bytes += sizeof(IPTR); +#endif + /* Overflow detection without SIZE_MAX */ + if (lSize + prefix_bytes < lSize) { + return NULL; + } + if (lSize + prefix_bytes + (MALLOC_ALIGN - 1) < lSize + prefix_bytes) { + return NULL; + } + + mallocSize = (lSize + prefix_bytes + (MALLOC_ALIGN - 1)) & ~(IPTR)(MALLOC_ALIGN - 1); + ADB(kprintf("[fryingpan] %s: adjusted size = %zu\n", __func__, mallocSize)); + + for (;;) { #ifndef __amigaos4__ - ObtainSemaphore(&__InternalSemaphore); // we wait only for our tasks! - pMem = (IPTR*)AllocPooled(__InternalMemPool, lSize); // Get memory - ReleaseSemaphore(&__InternalSemaphore); // and free sem. + ObtainSemaphore(&__InternalSemaphore); + raw = (void*)AllocPooled(__InternalMemPool, mallocSize); + ReleaseSemaphore(&__InternalSemaphore); #else - IExec->ObtainSemaphore(&__InternalSemaphore); // we wait only for our tasks! - pMem = (IPTR*)IExec->AllocPooled(__InternalMemPool, lSize); // Get memory - IExec->ReleaseSemaphore(&__InternalSemaphore); // and free sem. -#endif - if (0 != pMem) - { - pMem2 = &pMem[lSize>>2]; - pMem[0] = 0; // set mem pool - pMem[1] = lSize; // set mem size - pMem = &pMem[2+(16)]; // set mem handle - for (i=1; i<=(16); i++) - { - pMem[-i] = 0xC0DEDBAD; - pMem2[-i] = 0xC0DEDBAD; - } - } - - lSize -= (sizeof(IPTR) << 4); + IExec->ObtainSemaphore(&__InternalSemaphore); + raw = (void*)IExec->AllocPooled(__InternalMemPool, mallocSize); + IExec->ReleaseSemaphore(&__InternalSemaphore); #endif - } + if (raw == NULL) { + _error("Unable to allocate %zu bytes of memory!!!\nClick 'OK' to retry", lSize); + continue; + } - if (pMem == 0) - { - _error("Unable to allocate %ld bytes of memory!!!\nClick 'OK' to retry", lSize); - return malloc(lSize - (sizeof(IPTR) << 1)); - } - return pMem; // return mem :) -} + /* success: set up debug layout */ + { + IPTR *pMem2; + uint32 i; + IPTR base = (IPTR)raw; + IPTR addr = base + prefix_bytes; + addr = (addr + (MALLOC_ALIGN - 1)) & ~(IPTR)(MALLOC_ALIGN - 1); + + pMem = (void*)addr; /* user pointer */ + + /* Fill canaries: pMem[-1] .. pMem[-MEMDEBUG_CANARY_SLOTS] */ + for (i = 1; i <= (uint32)MEMDEBUG_CANARY_SLOTS; ++i) { + ((IPTR*)pMem)[-(int)i] = (IPTR)0xC0DEDBAD; + } + + /* metadata slots after canaries: */ + ((IPTR*)pMem)[-(MEMDEBUG_CANARY_SLOTS + 1)] = 0; + ((IPTR*)pMem)[-(MEMDEBUG_CANARY_SLOTS + 2)] = (IPTR)mallocSize; + ((IPTR*)pMem)[-(MEMDEBUG_CANARY_SLOTS + 3)] = (IPTR)raw; + + /* Mirror canaries at block end */ + pMem2 = (IPTR *)((IPTR)raw + mallocSize); + for (i = 1; i <= (uint32)MEMDEBUG_CANARY_SLOTS; ++i) { + pMem2[-(int)i] = (IPTR)0xC0DEDBAD; + } + + ADB(kprintf("[fryingpan] %s: alloc @ %p (raw %p)\n", __func__, pMem, raw)); + } + + break; /* done */ + } + +#endif /* MEMDEBUG */ + + return pMem; +} void* malloc_pooled(void* pPool, size_t lSize) { - IPTR *pMem = 0; + struct FPMemPrivate *priv; + IPTR *raw = NULL; + IPTR *pMem = NULL; + + if (lSize == 0) + return NULL; + + ADB(kprintf("[fryingpan] %s: requested size = %u\n", __func__, lSize)); - if (0 == lSize) - return 0; + // Reserve space for metadata + max alignment overhead + if (__InternalMemPool != NULL) + { + ADB(kprintf("[fryingpan] %s: internal pool @ 0x%p\n", __func__, __InternalMemPool)); - lSize = ((lSize + 7) & ~7) + (sizeof(IPTR) << 1); +#ifndef MEMDEBUG + // Round up requested size to alignment + size_t mallocSize = (lSize + sizeof(struct FPMemPrivate) + (MALLOC_ALIGN - 1)) & ~(MALLOC_ALIGN - 1); - if (0 != pPool) - { -#ifndef DEBUG #ifndef __amigaos4__ - pMem = (IPTR*)AllocPooled(pPool, lSize); // Get memory. I assume we dont need syncing here. + raw = (IPTR *)AllocPooled(pPool, mallocSize); #else - pMem = (IPTR*)IExec->AllocPooled(pPool, lSize); // Get memory. I assume we dont need syncing here. + raw = (IPTR *)IExec->AllocPooled(pPool, mallocSize); #endif - if (0 != pMem) - { - pMem[0] = (IPTR)pPool; // set mem pool - pMem[1] = lSize; // set mem size - pMem = &pMem[2]; // set mem handle - } + if (raw != NULL) + { + // Leave room for metadata and MALLOC_ALIGN the pointer + IPTR addr = (IPTR)raw + sizeof(struct FPMemPrivate); + addr = (addr + (MALLOC_ALIGN - 1)) & ~(MALLOC_ALIGN - 1); + priv = (struct FPMemPrivate *)addr - sizeof(struct FPMemPrivate); + pMem = (IPTR *)addr; + + ADB(kprintf("[fryingpan] %s: alloc @ 0x%p (raw 0x%p)\n", __func__, pMem, raw)); + + // Store metadata just before aligned address + priv->allocpool = pPool; + priv->allocwanted = lSize; + priv->allocsize = mallocSize; + priv->allocraw = raw; + } #else - uint32 i; - IPTR *pMem2 = 0; - lSize += (sizeof(IPTR) << 4); + size_t mallocSize = (lSize + (sizeof(IPTR) << 4) + (sizeof(IPTR) * 3) + (MALLOC_ALIGN - 1)) & ~(MALLOC_ALIGN - 1); #ifndef __amigaos4__ - pMem = (IPTR*)AllocPooled(pPool, lSize); // Get memory + raw = (IPTR*)AllocPooled(pPool, mallocSize); #else - pMem = (IPTR*)IExec->AllocPooled(pPool, lSize); // Get memory + raw = (IPTR*)IExec->AllocPooled(pPool, mallocSize); #endif + if (raw != NULL) + { + IPTR *pMem2; + uint32 i; + + IPTR addr = (IPTR)(raw + (sizeof(IPTR) << 5) + (sizeof(IPTR) * 3)); + addr = (addr + (MALLOC_ALIGN - 1)) & ~(MALLOC_ALIGN - 1); + pMem = (IPTR *)addr; - if (0 != pMem) - { - pMem2 = &pMem[lSize>>2]; - pMem[0] = (IPTR)pPool; // set mem pool - pMem[1] = lSize; // set mem size - pMem = &pMem[2+(16)]; // set mem handle - } - - for (i=1; i<=(16); i++) - { - pMem[-i] = 0xC0DEDBAD; - pMem2[-i] = 0xC0DEDBAD; - } + pMem[-17] = (IPTR)pPool; + pMem[-18] = mallocSize; + pMem[-19] = (IPTR)raw; + + pMem2 = (IPTR *)((IPTR)raw + mallocSize); + for (i=1; i<=(16); i++) + { + pMem[-i] = 0xC0DEDBAD; + pMem2[-i] = 0xC0DEDBAD; + } + } #endif - } - return pMem; // return mem :) -} + } + if (pMem == NULL) + { + _error("Unable to allocate %ld bytes of memory!!!\nClick 'OK' to retry", lSize); + return malloc(lSize); + } + + return pMem; +} FryingPan/framework/LibC/new.cpp | 81 ++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/FryingPan/framework/LibC/new.cpp b/FryingPan/framework/LibC/new.cpp index 29d2ea2ee..2072db091 100644 --- a/FryingPan/framework/LibC/new.cpp +++ b/FryingPan/framework/LibC/new.cpp @@ -19,63 +19,114 @@ #include "LibC.h" -static inline void* alloc_proxy(const struct Allocator*, size_t size) { - return malloc(size); +static inline void* alloc_proxy(const struct Allocator*a, size_t size) { + ADB(kprintf("[fryingpan] %s(0x%p, %ubytes)\n", __func__, a, size)); + return malloc(size); } -static inline void free_proxy(const struct Allocator*, void* mem) { - free(mem); +static inline void free_proxy(const struct Allocator*a, void* mem) { + ADB(kprintf("[fryingpan] %s(0x%p, 0x%p)\n", __func__, a, mem)); + free(mem); } const struct Allocator DEFAULT_ALLOCATOR = { &alloc_proxy, &free_proxy }; static const struct Allocator* current_allocator = &DEFAULT_ALLOCATOR; const struct Allocator* set_default_allocator(const struct Allocator *a) { - const struct Allocator *b = current_allocator; - current_allocator = a; + ADB(kprintf("[fryingpan] %s(0x%p)\n", __func__, a)); + const struct Allocator *b = current_allocator; + current_allocator = a; - return b; + return b; +} + +static inline void** alloc_proxy_aligned(const struct Allocator* a, + size_t size, size_t priv) +{ + ADB(kprintf("[fryingpan] %s(0x%p, %u, %u)\n", __func__, a, size, priv)); + size_t allocsize = size + priv; + +#if defined(AROS_WORSTALIGN) + allocsize = (allocsize + sizeof(void*) + (AROS_WORSTALIGN - 1)) & ~(AROS_WORSTALIGN - 1); +#endif + + ADB(kprintf("[fryingpan] %s: allocating %zu bytes\n", __func__, allocsize)); + void* membase = a->alloc(a, allocsize); + +#if defined(AROS_WORSTALIGN) + uintptr_t raw = reinterpret_cast(membase) + sizeof(void*) + priv; + uintptr_t aligned = (raw + (AROS_WORSTALIGN - 1)) & ~(AROS_WORSTALIGN - 1); + void** mem = reinterpret_cast(aligned - priv); + mem[-1] = membase; +#else + void** mem = reinterpret_cast(membase); +#endif + + ADB(kprintf("[fryingpan] %s: allocated @ 0x%p (returning @ 0x%p)\n", __func__, membase, mem)); + return mem; } void* operator new(size_t lSize) { - return operator new(lSize, current_allocator); + ADB(kprintf("[fryingpan] %s(%ubytes)\n", __func__, lSize)); + return operator new(lSize, current_allocator); } void* operator new[](size_t lSize) { - return operator new[](lSize, current_allocator); + ADB(kprintf("[fryingpan] %s(%ubytes)\n", __func__, lSize)); + return operator new[](lSize, current_allocator); } void* operator new(size_t lSize, const struct Allocator *a) { - void** mem = (void**)a->alloc(a, lSize + sizeof(IPTR)); - *mem++ = const_cast(a); - return mem; + ADB(kprintf("[fryingpan] %s(%ubytes, 0x%p)\n", __func__, lSize, a)); + void **mem = alloc_proxy_aligned(a, lSize, sizeof(void *)); + *mem++ = const_cast(a); + ADB(kprintf("[fryingpan] %s: allocated @ 0x%p\n", __func__, mem)); + return mem; } void* operator new[](size_t lSize, const struct Allocator *a) { - void** mem = (void**)a->alloc(a, lSize + sizeof(IPTR)); - *mem++ = const_cast(a); - return mem; + ADB(kprintf("[fryingpan] %s(%ubytes, 0x%p)\n", __func__, lSize, a)); + void **mem = alloc_proxy_aligned(a, lSize, sizeof(void *)); + *mem++ = const_cast(a); + ADB(kprintf("[fryingpan] %s: allocated @ 0x%p\n", __func__, mem)); + return mem; } void* operator new(size_t lSize, const Allocator* a, std::align_val_t align) noexcept { + + ADB(kprintf("[fryingpan] %s(%ubytes, 0x%p, %u)\n", __func__, lSize, a, align)); + std::size_t alignment = static_cast(align); std::size_t extra = alignment - 1 + sizeof(void*); + + ADB(kprintf("[fryingpan] %s: extra = %ubytes\n", __func__, extra)); + void* raw = a->alloc(a, lSize + extra); uintptr_t addr = reinterpret_cast(raw) + sizeof(void*); uintptr_t aligned = (addr + alignment - 1) & ~(alignment - 1); void** mem = reinterpret_cast(aligned); mem[-1] = raw; // store original pointer for deallocation + + ADB(kprintf("[fryingpan] %s: returning 0x%p\n", __func__, mem)); + return mem; } void* operator new(size_t lSize, std::align_val_t align) { + ADB(kprintf("[fryingpan] %s(%ubytes, %u)\n", __func__, lSize, align)); + return operator new(lSize, current_allocator, align); } void* operator new[](size_t lSize, const Allocator* a, std::align_val_t align) { + ADB(kprintf("[fryingpan] %s(%ubytes, %u)\n", __func__, lSize, align)); + return operator new(lSize, a, align); } void* operator new[](size_t lSize, std::align_val_t align) { + + ADB(kprintf("[fryingpan] %s(%ubytes, %u)\n", __func__, lSize, align)); + return operator new[](lSize, current_allocator, align); } FryingPan/framework/LibC/realloc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/FryingPan/framework/LibC/realloc.c b/FryingPan/framework/LibC/realloc.c index 8d48025dd..6ab0bbef5 100644 --- a/FryingPan/framework/LibC/realloc.c +++ b/FryingPan/framework/LibC/realloc.c @@ -28,14 +28,19 @@ void *realloc(void* mem, size_t len) // 8 extra bytes are included for memory block header (pool & len) if (0 == len) - return 0; + return NULL; + + //ADB( + kprintf("[fryingpan] %s(0x%p, %u)\n", __func__, mem, len); + //) if (NULL == mem) return newmem; #ifndef DEBUG - if (len > oldmem[-1]-8) - len = oldmem[-1]-8; + struct FPMemPrivate *priv = (struct FPMemPrivate *)((IPTR)oldmem - (IPTR)sizeof(struct FPMemPrivate)); + if (len > (priv->allocwanted)) + len = (priv->allocwanted); #else if (len > oldmem[-(1+(128>>3))]-8) len = oldmem[-(1+(128>>3))]-8; @@ -47,6 +52,7 @@ void *realloc(void* mem, size_t len) IExec->CopyMem(oldmem, newmem, len); #endif free(oldmem); + return newmem; }