Skip to content

Commit

Permalink
Change rebMalloc() to rebAllocBytes()
Browse files Browse the repository at this point in the history
The documentation spends a bunch of time explaining the differences
between malloc() and rebMalloc().  But really all we need is a name
that is different from rebAlloc() so that rebAlloc() can be a type
casting macro.

Naming it "rebAllocBytes()" discerned from "rebTryAllocBytes()" is
a better idea.
  • Loading branch information
hostilefork committed Feb 26, 2024
1 parent 5fe67e8 commit f13248e
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 72 deletions.
4 changes: 2 additions & 2 deletions extensions/crypt/mod-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ DECLARE_NATIVE(checksum)
static int Mpi_From_Binary(mbedtls_mpi* X, const Value* binary)
{
size_t size;
unsigned char* buf = rebBytes(&size, binary); // allocates w/rebMalloc()
unsigned char* buf = rebBytes(&size, binary); // allocates w/rebAlloc()

int result = mbedtls_mpi_read_binary(X, buf, size);

Expand Down Expand Up @@ -1286,7 +1286,7 @@ DECLARE_NATIVE(dh_compute_secret)
Value* peer_key = rebValue("peer-key");

size_t gy_size;
unsigned char* gy_buf = rebBytes(&gy_size, peer_key); // is a rebMalloc()
unsigned char* gy_buf = rebBytes(&gy_size, peer_key); // is a rebAlloc()

rebRelease(peer_key);

Expand Down
4 changes: 2 additions & 2 deletions extensions/filesystem/file-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ Value* Get_Current_Dir_Value(void)

size_t size = PATH_MAX - 1;
if (uv_cwd(path_utf8, &size) == UV_ENOBUFS) {
path_utf8 = cast(char*, rebRealloc(path_utf8, size)); // includes \0
path_utf8 = s_cast(rebReallocBytes(path_utf8, size)); // includes \0
size_t check = size;
uv_cwd(path_utf8, &check);
assert(check == size);
Expand Down Expand Up @@ -797,7 +797,7 @@ Value* Get_Current_Exec(void)

size_t size = PATH_MAX - 1;
if (uv_exepath(path_utf8, &size) == UV_ENOBUFS) {
path_utf8 = cast(char*, rebRealloc(path_utf8, size)); // includes \0
path_utf8 = s_cast(rebReallocBytes(path_utf8, size)); // includes \0
size_t check = size;
uv_exepath(path_utf8, &check);
assert(check == size);
Expand Down
2 changes: 1 addition & 1 deletion extensions/odbc/mod-odbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,7 @@ DECLARE_NATIVE(copy_odbc)
assert(len != SQL_NULL_DATA);
assert(len != SQL_NO_TOTAL);
assert(col->buffer == nullptr);
allocated = rebMalloc(len + 1); // can be rebRepossess()'d
allocated = rebAllocBytes(len + 1); // can be rebRepossess()'d
SQLLEN len_check;
rc = SQLGetData(
hstmt,
Expand Down
24 changes: 10 additions & 14 deletions extensions/process/call-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,11 @@ Bounce Call_Core(Level* level_) {

UNUSED(REF(info));

char *inbuf = nullptr;
unsigned char* inbuf = nullptr;
size_t inbuf_size = 0;
char *outbuf = nullptr;
unsigned char* outbuf = nullptr;
size_t outbuf_used = 0;
char *errbuf = nullptr;
unsigned char* errbuf = nullptr;
size_t errbuf_used = 0;

//=//// INPUT SOURCE SETUP ////////////////////////////////////////////=//
Expand Down Expand Up @@ -348,14 +348,14 @@ Bounce Call_Core(Level* level_) {
// *not* use those encodings, and transmit raw bytes.
//
inbuf_size = rebSpellInto(nullptr, 0, ARG(input));
inbuf = rebAllocN(char, inbuf_size + 1);
size_t check = rebSpellInto(inbuf, inbuf_size, ARG(input));
inbuf = rebAllocBytes(inbuf_size + 1);
size_t check = rebSpellInto(cast(char*, inbuf), inbuf_size, ARG(input));
assert(check == inbuf_size);
UNUSED(check);
goto input_via_buffer; }

case REB_BINARY: // feed standard input from BINARY! (full-band)
inbuf = s_cast(rebBytes(&inbuf_size, ARG(input)));
inbuf = rebBytes(&inbuf_size, ARG(input));

input_via_buffer:

Expand Down Expand Up @@ -469,14 +469,14 @@ Bounce Call_Core(Level* level_) {
outbuf_capacity = BUF_SIZE_CHUNK;
outbuf_used = 0;

outbuf = rebAllocN(char, outbuf_capacity);
outbuf = rebAllocBytes(outbuf_capacity);
handles[count++] = hOutputRead;
}
if (hErrorRead != NULL) {
errbuf_capacity = BUF_SIZE_CHUNK;
errbuf_used = 0;

errbuf = rebAllocN(char, errbuf_capacity);
errbuf = rebAllocBytes(errbuf_capacity);
handles[count++] = hErrorRead;
}

Expand Down Expand Up @@ -572,9 +572,7 @@ Bounce Call_Core(Level* level_) {
outbuf_used += n;
if (outbuf_used >= outbuf_capacity) {
outbuf_capacity += BUF_SIZE_CHUNK;
outbuf = cast(char*,
rebRealloc(outbuf, outbuf_capacity)
);
outbuf = rebReallocBytes(outbuf, outbuf_capacity);
if (outbuf == NULL) // !!! never with rebRealloc
goto kill;
}
Expand All @@ -601,9 +599,7 @@ Bounce Call_Core(Level* level_) {
errbuf_used += n;
if (errbuf_used >= errbuf_capacity) {
errbuf_capacity += BUF_SIZE_CHUNK;
errbuf = cast(char*,
rebRealloc(errbuf, errbuf_capacity)
);
errbuf = rebReallocBytes(errbuf, errbuf_capacity);
if (errbuf == NULL) // !!! never with rebRealloc
goto kill;
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/uuid/mod-uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ DECLARE_NATIVE(generate)
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(newId);
CFRelease(newId);

unsigned char* buf = (unsigned char*)(rebMalloc(16));
unsigned char* buf = rebAllocBytes(16);

buf[0] = bytes.byte0;
buf[1] = bytes.byte1;
Expand Down
59 changes: 29 additions & 30 deletions src/core/a-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ inline static const RebolValue* NULLIFY_NULLED(const Value* value) {


//
// rebMalloc: API
// rebAllocBytes: API
//
// * Unlike plain malloc(), this will fail() instead of return null if an
// allocation cannot be fulfilled.
// * Unlike something like malloc(), this will fail() instead of return null
// if an allocation cannot be fulfilled.
//
// * Like plain malloc(), if size is zero, the implementation just has to
// return something that free() will take. A backing series is added in
Expand All @@ -155,17 +155,17 @@ inline static const RebolValue* NULLIFY_NULLED(const Value* value) {
// * Because of the above points, null is *never* returned.
//
// * In order to make it possible to rebRepossess() the memory as a BINARY!
// that is then safe to alias as text, it always has an extra 0 byte at
// that is then safe to alias as TEXT!, it always has an extra 0 byte at
// the end of the data area.
//
// * It tries to be like malloc() by giving back a pointer "suitably aligned
// for the size of any fundamental type". See notes on ALIGN_SIZE.
//
// !!! rebAlignedMalloc() could exist to take an alignment, which could save
// on wasted bytes when ALIGN_SIZE > sizeof(Series*)...or work with "weird"
// large fundamental types that need more alignment than ALIGN_SIZE.
// !!! rebAllocBytesAligned() could exist to take an alignment, which could
// save on wasted bytes when ALIGN_SIZE > sizeof(Series*)...or work with
// "weird" large fundamental types that need more alignment than ALIGN_SIZE.
//
void* API_rebMalloc(size_t size)
unsigned char* API_rebAllocBytes(size_t size)
{
ENTER_API;

Expand All @@ -192,7 +192,7 @@ void* API_rebMalloc(size_t size)
//
// https://stackoverflow.com/a/37184840
//
// It may be that rebMalloc() and rebRealloc() should initialize with 0
// It may be that rebAalloc() and rebRealloc() should initialize with 0
// to defend against that, but that has a cost. For now we make no such
// promise--and leave it uninitialized so that address sanitizer notices
// when bytes are used that haven't been assigned.
Expand All @@ -203,18 +203,18 @@ void* API_rebMalloc(size_t size)
}

//
// rebTryMalloc: API
// rebTryAllocBytes: API
//
// Variant of rebMalloc() that returns nullptr on failure. To accomplish this
// it just uses a RESCUE_SCOPE to intercept any fail() that happens in the
// Variant of rebAllocBytes() that returns nullptr on failure. To accomplish
// this it just uses a RESCUE_SCOPE to intercept any fail() that happens in the
// course of the underlying series creation.
//
void* API_rebTryMalloc(size_t size)
unsigned char* API_rebTryAllocBytes(size_t size)
{
void* p;
Byte* p;

RESCUE_SCOPE_IN_CASE_OF_ABRUPT_FAILURE {
p = API_rebMalloc(size);
p = API_rebAllocBytes(size);
CLEANUP_BEFORE_EXITING_RESCUE_SCOPE;
return p;
} ON_ABRUPT_FAILURE (Context* e) {
Expand All @@ -225,30 +225,29 @@ void* API_rebTryMalloc(size_t size)


//
// rebRealloc: API
// rebReallocBytes: API
//
// * Like plain realloc(), null is legal for ptr (despite the fact that
// rebMalloc() never returns null, this can still be useful)
// * Like plain realloc(), null is legal for ptr
//
// * Like plain realloc(), it preserves the lesser of the old data range or
// the new data range, and memory usage drops if new_size is smaller:
//
// https://stackoverflow.com/a/9575348
//
// * Unlike plain realloc() (but like rebMalloc()), this fails instead of
// returning null, hence it's "safe" to say `ptr = rebRealloc(ptr, new_size)`
// * Unlike plain realloc() (but like rebAllocBytes()), this fails instead of
// returning null, so "safe" to say `ptr = rebReallocBytes(ptr, new_size)`
//
// * A 0 size is considered illegal. This is consistent with the C11 standard
// for realloc(), but not with malloc() or rebMalloc()...which allow it.
// for realloc(), but not with malloc() or rebAllocBytes()...which allow it.
//
void* API_rebRealloc(void *ptr, size_t new_size)
unsigned char* API_rebReallocBytes(void *ptr, size_t new_size)
{
ENTER_API;

assert(new_size > 0); // realloc() deprecated this as of C11 DR 400

if (not ptr) // C realloc() accepts null
return API_rebMalloc(new_size);
return API_rebAllocBytes(new_size);

Binary** ps = cast(Binary**, ptr) - 1;
Unpoison_Memory_If_Sanitize(ps, sizeof(Binary*)); // fetch `s` underruns
Expand All @@ -259,10 +258,10 @@ void* API_rebRealloc(void *ptr, size_t new_size)
REBLEN old_size = Binary_Len(s) - ALIGN_SIZE;

// !!! It's less efficient to create a new series with another call to
// rebMalloc(), but simpler for the time being. Switch to do this with
// rebAalloc(), but simpler for the time being. Switch to do this with
// the same series node.
//
void *reallocated = API_rebMalloc(new_size);
Byte* reallocated = API_rebAllocBytes(new_size);
memcpy(reallocated, ptr, old_size < new_size ? old_size : new_size);
API_rebFree(ptr);

Expand Down Expand Up @@ -349,7 +348,7 @@ void API_rebFree(void *ptr) {
//
// !!! All bytes in the allocation are expected to be initialized by this
// point, as failure to do so will mean reads crash the interpreter. See
// remarks in rebMalloc() about the issue, and possibly doing zero fills.
// remarks in rebAllocBytes() about the issue, and possibly doing zero fills.
//
// !!! It might seem tempting to use (Binary_Len(s) - ALIGN_SIZE). However,
// some routines make allocations bigger than they ultimately need and do not
Expand All @@ -370,7 +369,7 @@ RebolValue* API_rebRepossess(void* ptr, size_t size)
assert(Get_Series_Flag(s, DONT_RELOCATE));

if (size > Binary_Len(s) - ALIGN_SIZE)
fail ("Attempt to rebRepossess() more than rebMalloc() capacity");
fail ("Attempt to rebRepossess() more than rebAlloc() capacity");

Clear_Node_Root_Bit(s);
Clear_Series_Flag(s, DONT_RELOCATE);
Expand Down Expand Up @@ -1946,7 +1945,7 @@ REBWCHAR* API_rebSpellWideMaybe(

REBLEN len = Spell_Into_Wide(nullptr, 0, v);
REBWCHAR* result = cast(
REBWCHAR*, rebMalloc(sizeof(REBWCHAR) * (len + 1))
REBWCHAR*, rebAllocBytes(sizeof(REBWCHAR) * (len + 1))
);

REBLEN check = Spell_Into_Wide(result, len, v);
Expand Down Expand Up @@ -2155,7 +2154,7 @@ RebolValue* API_rebRescue(
//
// Variant of rebRescue() with a handler, similar to Ruby's rescue2 operation.
//
// 1. We want API allocations via rebValue() or rebMalloc() that occur in the
// 1. We want API allocations via rebValue() or rebAlloc() that occur in the
// body of the C function for the rebRescue() to be automatically cleaned
// up in the case of an error. There must be a frame to attach them to.
//
Expand Down Expand Up @@ -2540,7 +2539,7 @@ void API_rebUnmanage(void *p)

Node* n = cast(Node*, p);
if (Is_Node_A_Stub(n))
fail ("rebUnmanage() not yet implemented for rebMalloc() data");
fail ("rebUnmanage() not yet implemented for rebAlloc() data");

Value* v = cast(Value*, n);
assert(Is_Api_Value(v));
Expand Down
2 changes: 1 addition & 1 deletion src/core/c-state.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Rollback_Globals_To_State(struct Reb_State *s)
// Free any manual series that were extant (e.g. Make_Series() nodes
// which weren't created with NODE_FLAG_MANAGED and were not transitioned
// into the managed state). This will include the series used as backing
// store for rebMalloc() calls.
// store for rebAlloc() calls.
//
assert(Series_Dynamic_Used(g_gc.manuals) >= s->manuals_len);
while (Series_Dynamic_Used(g_gc.manuals) != s->manuals_len) {
Expand Down
8 changes: 4 additions & 4 deletions src/core/m-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ void Reify_Variadic_Feed_As_Array_Feed(
//
// Run_All_Handle_Cleaners: C
//
// !!! There's an issue with handles storing pointers to rebMalloc()'d data,
// !!! There's an issue with handles storing pointers to rebAlloc()'d data,
// which is that they want to do their cleanup work before the system is
// damaged by the shutdown process. This is a naive extra pass done during
// shutdown to deal with the problem--but it should be folded in with
Expand Down Expand Up @@ -646,7 +646,7 @@ static void Mark_Root_Series(void)

if (Is_Node_Root_Bit_Set(s)) {

// This stub came from Alloc_Value() or rebMalloc(); the only
// This stub came from Alloc_Value() or rebAlloc(); the only
// references should be from the C stack. So this pass is the
// only place where these stubs could be marked.

Expand All @@ -670,7 +670,7 @@ static void Mark_Root_Series(void)
//
Queue_Mark_Maybe_Fresh_Cell_Deep(Stub_Cell(s)); // [2]
}
else { // It's a rebMalloc()
else { // It's a rebAlloc()
assert(Series_Flavor(s) == FLAVOR_BINARY);
}

Expand Down Expand Up @@ -1021,7 +1021,7 @@ Count Sweep_Series(void)
// with Make_Series() and hasn't been managed. It doesn't
// participate in the GC. Leave it as is.
//
// (Alloc_Value() and rebMalloc() produce these by default)
// (Alloc_Value() and rebAlloc() produce these by default)
//
break;

Expand Down
16 changes: 8 additions & 8 deletions src/core/u-compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ static const int window_bits_zlib_raw = -(MAX_WBITS);

// Inflation and deflation tends to ultimately target series, so we want to
// be using memory that can be transitioned to a series without reallocation.
// See rebRepossess() for how rebMalloc()'d pointers can be used this way.
// See rebRepossess() for how rebAlloc()'d pointers can be used this way.
//
// We go ahead and use the rebMalloc() for zlib's internal state allocation
// We go ahead and use the rebAllocBytes() for zlib's internal state allocation
// too, so that any fail() calls (e.g. out-of-memory during a rebRealloc())
// will automatically free that state. Thus inflateEnd() and deflateEnd()
// only need to be called if there is no failure. There's no need to
Expand All @@ -91,10 +91,10 @@ static const int window_bits_zlib_raw = -(MAX_WBITS);
// As a side-benefit, fail() can be used freely for other errors during the
// inflate or deflate.

static void *zalloc(void *opaque, unsigned nr, unsigned size)
static void* zalloc(void *opaque, unsigned nr, unsigned size)
{
UNUSED(opaque);
return rebMalloc(nr * size);
return rebAllocBytes(nr * size);
}

static void zfree(void *opaque, void *addr)
Expand All @@ -109,7 +109,7 @@ static void zfree(void *opaque, void *addr)
//
static Context* Error_Compression(const z_stream *strm, int ret)
{
// rebMalloc() fails vs. returning nullptr, so as long as zalloc() is used
// rebAlloc() fails vs. returning nullptr, so as long as zalloc() is used
// then Z_MEM_ERROR should never happen.
//
assert(ret != Z_MEM_ERROR);
Expand Down Expand Up @@ -209,7 +209,7 @@ Byte* Compress_Alloc_Core(
//
assert(buf_size >= strm.total_out);
if (buf_size - strm.total_out > 1024)
output = cast(Byte*, rebRealloc(output, strm.total_out));
output = rebReallocBytes(output, strm.total_out);

deflateEnd(&strm); // done last (so strm variables can be read up to end)
return output;
Expand Down Expand Up @@ -347,7 +347,7 @@ Byte* Decompress_Alloc_Core(
if (max >= 0 and buf_size > cast(Size, max))
buf_size = max;

output = cast(Byte*, rebRealloc(output, buf_size));
output = rebReallocBytes(output, buf_size);

// Extending keeps the content but may realloc the pointer, so
// put it at the same spot to keep writing to
Expand All @@ -362,7 +362,7 @@ Byte* Decompress_Alloc_Core(
//
assert(buf_size >= strm.total_out);
if (strm.total_out - buf_size > 1024)
output = cast(Byte*, rebRealloc(output, strm.total_out));
output = rebReallocBytes(output, strm.total_out);

if (size_out)
*size_out = strm.total_out;
Expand Down
Loading

0 comments on commit f13248e

Please sign in to comment.