Skip to content

Commit

Permalink
Apparently realloc cannot be relied upon for acquiring available memory,
Browse files Browse the repository at this point in the history
Now using mmap as default means of acquiring memory, windows will have
to wait until I setup a VM to test it in (unless someone else feels like
putting in the effort to develop that bit)
  • Loading branch information
awsdert committed Oct 2, 2020
1 parent b6e757f commit 6a204dc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 74 deletions.
7 changes: 4 additions & 3 deletions mak/dst_cc.mak
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ F_fPIE:=$(call ifin,$(CC),vc,,-fPIE)
F_g_gdb:=$(call ifin,$(CC),vc,,-ggdb)
F_pg:=$(call ifin,$(CC),vc,,-pg)
F_pedantic:=$(call ifin,$(CC),vc,,-pedantic)
F_santize-address:=$(call ifin,$(CC),vc,,-fsanitize=address)
F_L:=$(COP)L
F_l:=$(COP)l
F_I:=$(COP)I
Expand All @@ -42,7 +43,7 @@ PFL_SFX:=_p
endif

ifeq '$(filter debug,$(MAKECMDGOALS))' 'debug'
DBG_FLAGS:=$(F_g_gdb) $(F_D) _DEBUG $(F_O)0
DBG_FLAGS:=$(F_g_gdb) $(F_D) _DEBUG $(F_O)0 $(F_santize-address)
DBG_APP:=$(GDB) $(COP)ex run
DBG_SFX:=_d
# Debugging takes precedence over profiling
Expand All @@ -52,7 +53,7 @@ PFL_SFX:=
endif

ifeq '$(filter gede,$(MAKECMDGOALS))' 'gede'
DBG_FLAGS:=$(F_g_gdb) $(F_D) _DEBUG $(F_O)0
DBG_FLAGS:=$(F_g_gdb) $(F_D) _DEBUG $(F_O)0 $(F_santize-address)
DBG_APP:=$(GDB) $(COP)ex run
DBG_SFX:=_d
# Debugging takes precedence over profiling
Expand All @@ -62,7 +63,7 @@ PFL_SFX:=
endif

ifeq '$(filter check,$(MAKECMDGOALS))' 'check'
DBG_FLAGS:=$(F_g_gdb) $(F_D) _DEBUG $(F_O)0
DBG_FLAGS:=$(F_g_gdb) $(F_D) _DEBUG $(F_O)0 $(F_santize-address)
DBG_APP:=$(GDB) $(COP)ex run
DBG_SFX:=_d
# Debugging takes precedence over profiling
Expand Down
7 changes: 5 additions & 2 deletions src/alu_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ int_t alu_setup_reg( alu_t *alu, uint_t want, size_t Nsize )

return alu_err_null_ptr( "alu" );
}
#include <assert.h>

uint_t alu_get_reg_node( alu_t *alu, size_t Nsize )
{
Expand All @@ -186,18 +187,20 @@ uint_t alu_get_reg_node( alu_t *alu, size_t Nsize )

alu_errno(alu) = alu_setup_reg( alu, reg, Nsize );

if ( !alu_errno(alu) )
if ( alu->given >= count )
{
alu->taken = count;

(void)memset( alu_data( alu, index ), 0, Nsize );
(void)memset( alu_data( alu, index ), 0, alu->Nsize );

alu_set_active( alu, index );

return index;
}

alu->block.fault = SET2IF( alu_errno(alu), alu_errno(alu), ERANGE );
alu_error( alu_errno(alu) );
alu_printf( "given = %u, needed %u", alu->given, count );
return 0;
}

Expand Down
121 changes: 53 additions & 68 deletions src/alu_mem.c
Original file line number Diff line number Diff line change
@@ -1,99 +1,84 @@
#include "alu.h"
#include <stdlib.h>
#include <string.h>
#ifdef UNIC_SYS_WIN32
#else
#include <unistd.h>
#include <sys/mman.h>
#endif

void* alu_block( alu_block_t *mem, size_t want, int_t dir )
{
int ret = EINVAL;
uchar_t *block = NULL;
size_t size;
size_t size, pagesize;
#ifdef UNIC_SYS_WIN32
SYSTEM_INFO si;
#endif

if ( mem )
{
mem->fault = 0;

if ( mem->block )
if ( want )
{
#ifdef UNIC_SYS_WIN32
GetSystemInfo(&si);
pagesize = si.dwPageSize
#else
pagesize = sysconf(_SC_PAGESIZE);
#endif

want = SET2IF( dir > 0, HIGHEST( mem->given, want ), want );
want = SET2IF( dir < 0, LOWEST( mem->given, want ), want );

if ( want )
{
size = want % sizeof(size_t);
want /= SET2IF( size, sizeof(size_t), 1 );
want *= SET2IF( size, sizeof(size_t), 1 );
want += SET1IF( size, sizeof(size_t) );

if ( want != mem->given )
{
size = want + (4 * sizeof(size_t));
alu_printf
(
"want = %zu, size = %zu, given = %zu"
, want
, size
, mem->given
);

errno = 0;
block = realloc( mem->block, size );
ret = errno;
}
else
{
return mem->block;
}
}
else
{
free( mem->block );
(void)memset( mem, 0, sizeof(alu_block_t) );
return NULL;
}
}
else
{
want = SET1IF( dir >= 0, want );

memset( mem, 0, sizeof(alu_block_t) );
size = want % pagesize;
want /= pagesize;
want += !!size;
want *= pagesize;

if ( want )
{
size = want % sizeof(size_t);
want /= SET2IF( size, sizeof(size_t), 1 );
want *= SET2IF( size, sizeof(size_t), 1 );
want += SET1IF( size, sizeof(size_t) );

size = want + (4 * sizeof(size_t));
alu_printf
if ( want != mem->given )
{
errno = 0;
block = mmap
(
"want = %zu, size = %zu, given = %zu"
NULL
, want
, size
, mem->given
, PROT_READ | PROT_WRITE
, MAP_PRIVATE | MAP_ANON
, -1
, 0
);

errno = 0;
block = malloc( size );
ret = errno;

if ( block )
{
if ( mem->block )
{
size = LOWEST( want, mem->given );
memcpy( block, mem->block, size );
munmap( mem->block, mem->given );
}

mem->given = want;
mem->taken = LOWEST( want, mem->taken );
return (mem->block = block);
}

mem->fault = ret;
return NULL;
}
else
{
return mem->block;
}

return mem->block;
}

if ( block )
{
mem->block = block;
mem->given = want;

for ( size = want; size > mem->taken; block[--size] = 0 );

return block;
if ( mem->block )
{
munmap( mem->block, mem->given );
}

mem->fault = ret;
(void)memset( mem, 0, sizeof(alu_block_t) );
return NULL;
}

Expand Down
6 changes: 5 additions & 1 deletion src/alu_vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ void* alu_vec( alu_vec_t *vec, uint_t want, size_t Nsize, int dir )
used = vec->taken;
vec->block.taken = used * vec->Nsize;

if ( Nsize >= vec->Nsize )
if ( Nsize == vec->Nsize )
{
return vec->block.block;
}
else if ( Nsize > vec->Nsize )
{
block = alu_block( &(vec->block), desire, dir );

Expand Down

0 comments on commit 6a204dc

Please sign in to comment.