Skip to content

Commit

Permalink
libbcs: update functions to use void[]* when applicable instead of …
Browse files Browse the repository at this point in the history
…`int`.
  • Loading branch information
TDRRmk2 committed Dec 10, 2024
1 parent 3d832ef commit 14bb9b2
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/libbcs.bcs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal struct HeapHead
};

// Main memory allocation function. Returns a pointer to a buffer of the requested size.
internal int malloc (int size)
internal void[]* malloc (int size)
{
struct HeapHead PTR_SPC* head;
struct AllocBlock PTR_SPC* block;
Expand Down Expand Up @@ -137,15 +137,15 @@ internal int malloc (int size)
}

// Frees the memory so it is usable by future calls to malloc.
internal void free (int ptr)
internal void free (void[]* ptr)
{
if(!ptr)
return;

struct HeapHead PTR_SPC* head;
struct AllocBlock PTR_SPC* block, nextBlock;

block = ptr - lengthof(block);
block = (int)ptr - lengthof(block);
nextBlock = block.next;

// Should merge blocks here.
Expand All @@ -161,28 +161,28 @@ internal void free (int ptr)
head.lastFreed = block;
}

internal void memmove (int dstIn, int srcIn, int size)
internal void memmove (void[]* dstIn, void[]* srcIn, int size)
{
int[] PTR_SPC* dst = dstIn;
int[] PTR_SPC* src = srcIn;

if(size < 1)
return;

if(dstIn < srcIn)
if((int)dstIn < (int)srcIn)
{
for(int i = 0; i < size; i++)
dst[i] = src[i];
}
else if(dstIn > srcIn)
else if((int)dstIn > (int)srcIn)
{
for(int i = size - 1; i --> 0;)
dst[i] = src[i];
}
}

// Creates a new bigger buffer if needed, copying the contents of the original passed in.
internal int realloc (int oldPtr, int size)
internal void[]* realloc (void[]* oldPtr, int size)
{
if(!oldPtr)
return malloc(size);
Expand All @@ -195,13 +195,13 @@ internal int realloc (int oldPtr, int size)

struct AllocBlock PTR_SPC* oldBlock;

oldBlock = oldPtr - lengthof(oldBlock);
oldBlock = (int)oldPtr - lengthof(oldBlock);

if(oldBlock.size >= size)
return oldPtr;

int ptr = malloc(size);
Log(s:"realloc new ptr is ", i:ptr);
void[]* ptr = malloc(size);
Log(s:"realloc new ptr is ", i:(int)ptr);

memmove(ptr, oldPtr, oldBlock.size);

Expand All @@ -218,11 +218,11 @@ Script _LIBBCS_REV "-alloca" (int ptr)
}
}

internal int alloca (int size)
internal void[]* alloca (int size)
{
int ptr = malloc(size);
void[]* ptr = malloc(size);

__libbcs_CallACS(_LIBBCS_REV "-alloca", ptr);
__libbcs_CallACS(_LIBBCS_REV "-alloca", (int)ptr);

return ptr;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ internal void printArrPtr (void[] PTR_SPC? arr)

internal void[] PTR_SPC? arrNew (int len, int elementSize = 1)
{
int ptr = malloc((len + 1 + LIBBCS_ARR_EXTRA) * elementSize);
int ptr = (int)malloc((len + 1 + LIBBCS_ARR_EXTRA) * elementSize);

return makeArrPtr(ptr, len);
}
Expand All @@ -301,15 +301,15 @@ internal void[] PTR_SPC? arrResize (void[] PTR_SPC? arr, int newSize, int elemen
if(arrBlock.size >= newSize)
return makeArrPtr((int)arr - 1, newSize * elementSize);

int newPtr = realloc((int)arr - 1, (newSize + 1 + LIBBCS_ARR_EXTRA) * elementSize);
void[]* newPtr = realloc((int)arr - 1, (newSize + 1 + LIBBCS_ARR_EXTRA) * elementSize);

if(!newPtr)
{
Log(s:"\ckDynarray WARNING: resize to ", i:newSize, s:" FAILED!");
return arr;
}

return makeArrPtr(newPtr, newSize * elementSize);
return makeArrPtr((int)newPtr, newSize * elementSize);
}

#if 1
Expand Down

0 comments on commit 14bb9b2

Please sign in to comment.