Skip to content

feat: Add C strdup() #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion interop/nkic/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Released under Apache 2.0 license as described in the file LICENSE.
#include "region.h"
#include "stdc.h"

#include <stdio.h>

struct block {
size_t size;
size_t offset;
Expand Down Expand Up @@ -51,7 +53,7 @@ void region_destroy(struct region *region) {
}
}

void *region_alloc(struct region *region, size_t size) {
void *region_try_alloc(struct region *region, size_t size) {
if (unlikely(!region))
return NULL;

Expand All @@ -78,3 +80,34 @@ void *region_alloc(struct region *region, size_t size) {
b->offset += size;
return p;
}

void *region_alloc(struct region *region, size_t size) {
void *p = region_try_alloc(region, size);
if (unlikely(!p)) {
fprintf(stderr, "Out Of Memory. NKI compiler will abort the program.\n");
abort();
}

return p;
}

char *region_strdup(struct region *region, const char *src) {
assert(src);
size_t size = strlen(src) + 1;
char *dst = region_alloc(region, size);
// copy everything, including null-terminator
memcpy(dst, src, size);
return dst;
}

char *region_strndup(struct region *region, const char *src, size_t len) {
assert(src);

// check for null-terminator earlier than `len`
len = strnlen(src, len);

char *dst = region_alloc(region, len + 1);
memcpy(dst, src, len);
dst[len] = 0; // add null-terminator
return dst;
}
16 changes: 15 additions & 1 deletion interop/nkic/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ Released under Apache 2.0 license as described in the file LICENSE.

struct region;

struct region *region_create();
struct region *region_create(void);
void region_destroy(struct region *region);

// Return newly allocated memory, or NULL if allocation fails.
// Note that ALL OTHER allocation functions will abort if allocation fails;
// they will never return NULL.
void *region_try_alloc(struct region *region, size_t size);

// Return newly allocated memory.
void *region_alloc(struct region *region, size_t size);

// Return newly allocated copy of null-terminated string.
char *region_strdup(struct region *region, const char *src);

// Return newly allocated copy of string, with at most `len` bytes copied.
// Copying stops when `len` bytes are copied, or a null-terminator is found.
// The new string is always null-terminated.
char *region_strndup(struct region *region, const char *src, size_t len);