From 05fa29ba8bc2dbfaa9af7ed1263554c7cdc9214d Mon Sep 17 00:00:00 2001 From: Chris Young Date: Sat, 19 Nov 2016 21:40:32 +0000 Subject: more allocvec/malloc changes --- frontends/amiga/os3support.c | 317 +++++++++++++++++++++---------------------- 1 file changed, 158 insertions(+), 159 deletions(-) (limited to 'frontends/amiga/os3support.c') diff --git a/frontends/amiga/os3support.c b/frontends/amiga/os3support.c index bdf633316..c08260209 100644 --- a/frontends/amiga/os3support.c +++ b/frontends/amiga/os3support.c @@ -46,6 +46,155 @@ #define FAILURE (FALSE) #define NO ! +/* Utility */ +struct FormatContext +{ + STRPTR Index; + LONG Size; + BOOL Overflow; +}; + +STATIC VOID ASM +StuffChar( + REG(a3, struct FormatContext * Context), + REG(d0, UBYTE Char)) +{ + /* Is there still room? */ + if(Context->Size > 0) + { + (*Context->Index) = Char; + + Context->Index++; + Context->Size--; + + /* Is there only a single character left? */ + if(Context->Size == 1) + { + /* Provide null-termination. */ + (*Context->Index) = '\0'; + + /* Don't store any further characters. */ + Context->Size = 0; + } + } + else + { + Context->Overflow = TRUE; + } +} + +BOOL +VSPrintfN( + LONG MaxLen, + STRPTR Buffer, + const STRPTR FormatString, + const va_list VarArgs) +{ + BOOL result = FAILURE; + + /* format a text, but place only up to MaxLen + * characters in the output buffer (including + * the terminating NUL) + */ + + if (Buffer == NULL || FormatString == NULL) return(result); + + if(MaxLen > 1) + { + struct FormatContext Context; + + Context.Index = Buffer; + Context.Size = MaxLen; + Context.Overflow = FALSE; + + RawDoFmt(FormatString,(APTR)VarArgs,(VOID (*)())StuffChar,(APTR)&Context); + + if(NO Context.Overflow) + result = SUCCESS; + } + + return(result); +} + +BOOL +SPrintfN( + LONG MaxLen, + STRPTR Buffer, + const STRPTR FormatString, + ...) +{ + va_list VarArgs; + BOOL result = FAILURE; + + /* format a text, varargs version */ + + if (Buffer == NULL && FormatString == NULL) return result; + + va_start(VarArgs,FormatString); + result = VSPrintfN(MaxLen,Buffer,FormatString,VarArgs); + va_end(VarArgs); + + return(result); +} + +char *ASPrintf(const char *fmt, ...) +{ + int r; + va_list ap; + static char buffer[2048]; + char *rbuf; + + va_start(ap, fmt); + r = VSPrintfN(2048, buffer, (const STRPTR)fmt, ap); + va_end(ap); + + r = strlen(buffer); + rbuf = AllocVec(r+1, MEMF_CLEAR); + if (rbuf != NULL) + { + strncpy(rbuf, buffer, r); + } + return rbuf; +} + +/* C */ +char *strlwr(char *str) +{ + size_t i; + size_t len = strlen(str); + + for(i=0; iti_Tag != OT_FileIdent) || (tag->ti_Data != (ULONG)size)) { LOG("Invalid OTAG file"); - FreeVec(buffer); + free(buffer); FreeVec(otagpath); return NULL; } @@ -132,7 +281,7 @@ struct OutlineFont *OpenOutlineFont(STRPTR fileName, struct List *list, ULONG fl fname = ASPrintf("%s.library", ti->ti_Data); } else { LOG("Cannot find OT_Engine tag"); - FreeVec(buffer); + free(buffer); FreeVec(otagpath); return NULL; } @@ -141,7 +290,7 @@ struct OutlineFont *OpenOutlineFont(STRPTR fileName, struct List *list, ULONG fl if(BulletBase == NULL) { LOG("Unable to open font engine %s", fname); - FreeVec(buffer); + free(buffer); FreeVec(fname); FreeVec(otagpath); } @@ -155,7 +304,7 @@ struct OutlineFont *OpenOutlineFont(STRPTR fileName, struct List *list, ULONG fl OT_OTagList, (ULONG)buffer, TAG_DONE); - of = AllocVec(sizeof(struct OutlineFont), MEMF_CLEAR); + of = calloc(1, sizeof(struct OutlineFont)); if(of == NULL) return NULL; of->BulletBase = BulletBase; @@ -174,8 +323,8 @@ void CloseOutlineFont(struct OutlineFont *of, struct List *list) CloseLibrary((struct Library *)BulletBase); FreeVec(of->OTagPath); - FreeVec(of->olf_OTagList); - FreeVec(of); + free(of->olf_OTagList); + free(of); } @@ -183,13 +332,13 @@ void CloseOutlineFont(struct OutlineFont *of, struct List *list) int64 GetFileSize(BPTR fh) { int32 size = 0; - struct FileInfoBlock *fib = AllocVec(sizeof(struct FileInfoBlock), MEMF_ANY); + struct FileInfoBlock *fib = malloc(sizeof(struct FileInfoBlock)); if(fib == NULL) return 0; ExamineFH(fh, fib); size = fib->fib_Size; - FreeVec(fib); + free(fib); return (int64)size; } @@ -280,155 +429,5 @@ APTR NewObject(struct IClass * classPtr, CONST_STRPTR classID, ULONG tagList, .. { return NewObjectA(classPtr, classID, (const struct TagItem *) &tagList); } - -/* Utility */ -struct FormatContext -{ - STRPTR Index; - LONG Size; - BOOL Overflow; -}; - -STATIC VOID ASM -StuffChar( - REG(a3, struct FormatContext * Context), - REG(d0, UBYTE Char)) -{ - /* Is there still room? */ - if(Context->Size > 0) - { - (*Context->Index) = Char; - - Context->Index++; - Context->Size--; - - /* Is there only a single character left? */ - if(Context->Size == 1) - { - /* Provide null-termination. */ - (*Context->Index) = '\0'; - - /* Don't store any further characters. */ - Context->Size = 0; - } - } - else - { - Context->Overflow = TRUE; - } -} - -BOOL -VSPrintfN( - LONG MaxLen, - STRPTR Buffer, - const STRPTR FormatString, - const va_list VarArgs) -{ - BOOL result = FAILURE; - - /* format a text, but place only up to MaxLen - * characters in the output buffer (including - * the terminating NUL) - */ - - if (Buffer == NULL || FormatString == NULL) return(result); - - if(MaxLen > 1) - { - struct FormatContext Context; - - Context.Index = Buffer; - Context.Size = MaxLen; - Context.Overflow = FALSE; - - RawDoFmt(FormatString,(APTR)VarArgs,(VOID (*)())StuffChar,(APTR)&Context); - - if(NO Context.Overflow) - result = SUCCESS; - } - - return(result); -} - -BOOL -SPrintfN( - LONG MaxLen, - STRPTR Buffer, - const STRPTR FormatString, - ...) -{ - va_list VarArgs; - BOOL result = FAILURE; - - /* format a text, varargs version */ - - if (Buffer == NULL && FormatString == NULL) return result; - - va_start(VarArgs,FormatString); - result = VSPrintfN(MaxLen,Buffer,FormatString,VarArgs); - va_end(VarArgs); - - return(result); -} - -char *ASPrintf(const char *fmt, ...) -{ - int r; - va_list ap; - static char buffer[2048]; - char *rbuf; - - va_start(ap, fmt); - r = VSPrintfN(2048, buffer, (const STRPTR)fmt, ap); - va_end(ap); - - r = strlen(buffer); - rbuf = AllocVec(r+1, MEMF_CLEAR); - if (rbuf != NULL) - { - strncpy(rbuf, buffer, r); - } - return rbuf; -} - -/* C */ -char *strlwr(char *str) -{ - size_t i; - size_t len = strlen(str); - - for(i=0; i