Skip to content

Commit

Permalink
CHANGE: using calloc instead of malloc+memset when allocating a…
Browse files Browse the repository at this point in the history
… zeroed memory
  • Loading branch information
Oldes committed Oct 2, 2024
1 parent 3994af8 commit 666bc51
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/core/b-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,9 @@ static void Set_Option_File(REBCNT field, REBYTE* src, REBOOL dir )
PG_Boot_Level = BOOT_LEVEL_FULL;
PG_Mem_Usage = 0;
PG_Mem_Limit = 0;
PG_Reb_Stats = Make_Mem(sizeof(*PG_Reb_Stats));
PG_Reb_Stats = Make_CMem(sizeof(*PG_Reb_Stats));
Halt_State = 0;
Reb_Opts = Make_Mem(sizeof(*Reb_Opts));
Reb_Opts = Make_CMem(sizeof(*Reb_Opts));

// Thread locals:
Trace_Level = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/c-port.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ SCHEME_ACTIONS *Scheme_Actions; // Initial Global (not threaded)
// Register a handle used to hold native port states
Register_Handle(SYM_PORT_STATEX, sizeof(REBREQ), NULL);

Scheme_Actions = Make_Mem(sizeof(SCHEME_ACTIONS) * MAX_SCHEMES);
Scheme_Actions = Make_Clear_Mem(sizeof(SCHEME_ACTIONS), MAX_SCHEMES);

Init_Console_Scheme();
Init_File_Scheme();
Expand Down
4 changes: 2 additions & 2 deletions src/core/m-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,11 +777,11 @@ static void Mark_Series(REBSER *series, REBCNT depth);
GC_Disabled = 0; // GC disabled counter for critical sections.
GC_Ballast = MEM_BALLAST;
GC_Last_Infant = 0; // Keep the last N series safe from GC.
GC_Infants = Make_Mem((MAX_SAFE_SERIES + 2) * sizeof(REBSER*)); // extra
GC_Infants = Make_Clear_Mem(sizeof(REBSER*), (MAX_SAFE_SERIES + 2)); // extra

Init_Pools(scale);

Prior_Expand = Make_Mem(MAX_EXPAND_LIST * sizeof(REBSER*));
Prior_Expand = Make_Clear_Mem(sizeof(REBSER*), MAX_EXPAND_LIST);
Prior_Expand[0] = (REBSER*)1;

// Temporary series protected from GC. Holds series pointers.
Expand Down
43 changes: 36 additions & 7 deletions src/core/m-pools.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
*/ void *Make_Mem(size_t size)
/*
** Main memory allocation wrapper function.
** NOTE: use Make_Clear_Mem if you need zeroed memory!
**
***********************************************************************/
{
Expand All @@ -129,11 +130,40 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
if (PG_Mem_Limit != 0 && (PG_Mem_Usage > PG_Mem_Limit)) {
Check_Security(SYM_MEMORY, POL_EXEC, 0);
}
CLEAR(ptr, size);

return ptr;
}

/***********************************************************************
**
*/ void *Make_Clear_Mem(size_t nmemb, size_t size)
/*
** Memory allocation wrapper around `calloc` function.
**
***********************************************************************/
{
void *ptr;

if (!(ptr = calloc(nmemb, size))) return 0;
PG_Mem_Usage += (size * nmemb);
if (PG_Mem_Limit != 0 && (PG_Mem_Usage > PG_Mem_Limit)) {
Check_Security(SYM_MEMORY, POL_EXEC, 0);
}

return ptr;
}

/***********************************************************************
**
*/ FORCE_INLINE void *Make_CMem(size_t size)
/*
** Memory allocation wrapper around `calloc` function.
**
***********************************************************************/
{
return Make_Clear_Mem(size, 1);
}


/***********************************************************************
**
Expand Down Expand Up @@ -161,15 +191,15 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
else if (scale < 0) unscale = -scale, scale = 1;

// Copy pool sizes to new pool structure:
Mem_Pools = Make_Mem(sizeof(REBPOL) * MAX_POOLS);
Mem_Pools = Make_Clear_Mem(sizeof(REBPOL), MAX_POOLS);
for (n = 0; n < MAX_POOLS; n++) {
Mem_Pools[n].wide = Mem_Pool_Spec[n].wide;
Mem_Pools[n].units = (Mem_Pool_Spec[n].units * scale) / unscale;
if (Mem_Pools[n].units < 2) Mem_Pools[n].units = 2;
}

// For pool lookup. Maps size to pool index. (See Find_Pool below)
PG_Pool_Map = Make_Mem((4 * MEM_BIG_SIZE) + 4); // extra
PG_Pool_Map = Make_CMem((4 * MEM_BIG_SIZE) + 4); // extra
n = 9; // sizes 0 - 8 are pool 0
for (; n <= 16 * MEM_MIN_SIZE; n++) PG_Pool_Map[n] = MEM_TINY_POOL + ((n-1) / MEM_MIN_SIZE);
for (; n <= LAST_SMALL_SIZE * MEM_MIN_SIZE; n++) PG_Pool_Map[n] = MEM_SMALL_POOLS-4 + ((n-1) / (MEM_MIN_SIZE * 4));
Expand Down Expand Up @@ -232,10 +262,9 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
REBCNT mem_size = pool->wide * units + sizeof(REBSEG);
#endif

seg = (REBSEG *) Make_Mem(mem_size);
seg = (REBSEG *) Make_CMem(mem_size);
if (!seg) Crash(RP_NO_MEMORY, mem_size);

CLEAR(seg, mem_size); // needed to clear series nodes
seg->size = mem_size;
seg->next = pool->segs;
pool->segs = seg;
Expand Down Expand Up @@ -350,7 +379,7 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
#ifdef MUNGWALL
node = (REBNOD *) Make_Mem(length+2*MUNG_SIZE);
#else
node = (REBNOD *) Make_Mem(length);
node = (REBNOD *) Make_CMem(length);
#endif
if (!node) Trap0(RE_NO_MEMORY);
#ifdef MUNGWALL
Expand Down Expand Up @@ -428,7 +457,7 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
#ifdef MUNGWALL
node = (REBNOD *) Make_Mem(length+2*MUNG_SIZE);
#else
node = (REBNOD *) Make_Mem(length);
node = (REBNOD *) Make_CMem(length);
#endif
if (!node) {
Free_Node(SERIES_POOL, (REBNOD *)series);
Expand Down
2 changes: 1 addition & 1 deletion src/core/n-strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ static struct digest {
}
REBYTE tmpdigest[128]; // Size must be max of all digest[].len;
REBYTE ipad[128],opad[128]; // Size must be max of all digest[].hmacblock;
void *ctx = Make_Mem(digests[i].ctxsize());
void *ctx = Make_CMem(digests[i].ctxsize());

REBCNT blocklen = digests[i].hmacblock;

Expand Down
4 changes: 2 additions & 2 deletions src/core/s-mold.c
Original file line number Diff line number Diff line change
Expand Up @@ -1643,14 +1643,14 @@ STOID Mold_Error(REBVAL *value, REB_MOLD *mold, REBFLG molded)
Set_Root_Series(TASK_BUF_MOLD, Make_Unicode(size), cb_cast("mold buffer"));

// Create quoted char escape table:
Char_Escapes = cp = Make_Mem(MAX_ESC_CHAR+1); // cleared
Char_Escapes = cp = Make_CMem(MAX_ESC_CHAR+1); // cleared
for (c = '@'; c <= '_'; c++) *cp++ = c;
Char_Escapes[TAB] = '-';
Char_Escapes[LF] = '/';
Char_Escapes['"'] = '"';
Char_Escapes['^'] = '^';

URL_Escapes = cp = Make_Mem(MAX_URL_CHAR+1); // cleared
URL_Escapes = cp = Make_CMem(MAX_URL_CHAR+1); // cleared
// escape all chars from #"^(00)" to #"^(20)"
for (c = 0; c <= ' '; c++) cp[c] = ESC_URL | ESC_FILE;
// and also all chars which are a lexer delimiters + 3 common extra chars
Expand Down

0 comments on commit 666bc51

Please sign in to comment.