Initial commit, stdint, added comments

This commit is contained in:
2026-03-08 16:21:05 -05:00
commit 2716e96d7a
10 changed files with 637 additions and 0 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

76
Makefile Normal file
View File

@@ -0,0 +1,76 @@
# Compiler Flags
CC := gcc
CFLAGS := -g -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer
# Directory variables
LIBDIR := lib
OBJ := obj
INC := include
SRC := src
TEST := tests
# Filepath Pattern Matching
LIB := $(LIBDIR)/lib.a
SRCS := $(wildcard $(SRC)/*.c)
OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
TESTS := $(wildcard $(TEST)/*.c)
TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS))
# Commands must be labeled PHONY
.PHONY: all release clean test
# Compiler Release Flags
release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG
release: clean $(LIB)
# Target for compilation.
all: $(LIB)
# Target / Dependencies
$(LIB): $(OBJS) | $(LIBDIR)
$(RM) $(LIB)
ar -cvrs $@ $^
$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJ)/%.o: $(SRC)/%.c | $(OBJ)
$(CC) $(CFLAGS) -c $< -o $@
$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin
$(CC) $(CFLAGS) $< $(LIB) -o $@
# Make directories if none.
$(LIBDIR):
mkdir $@
$(INC):
mkdir $@
$(OBJ):
mkdir $@
$(TEST)/bin:
mkdir $@
# Run the tests in the bin folder and track results
test: $(LIB) $(TEST)/bin $(TESTBINS)
@SUCCESS_COUNT=0; FAILURE_COUNT=0; \
for test in $(TESTBINS); do \
./$$test; \
EXIT_CODE=$$?; \
TEST_NAME=$(notdir $$test); \
if [ $$EXIT_CODE -eq 0 ]; then \
echo "\033[0;32m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (SUCCESS)\033[0m"; \
SUCCESS_COUNT=$$((SUCCESS_COUNT + 1)); \
else \
echo "\033[0;31m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (FAILURE)\033[0m"; \
FAILURE_COUNT=$$((FAILURE_COUNT + 1)); \
fi; \
done; \
echo "\n\nTests completed"; \
echo "SUCCESS: $$SUCCESS_COUNT"; \
echo "FAILURE: $$FAILURE_COUNT";
clean:
$(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/

28
README.md Normal file
View File

@@ -0,0 +1,28 @@
# atom
## Description
Simple string interning library. Interning strings will optimize memory usage and prevent duplicates, allowing for faster comparison and reduced memory overhead. This is useful when you need to store many identical strings (such as identifiers or constants) but want to avoid storing them multiple times.
## Table of Contents
- [Description](#description)
- [Features](#features)
- [Usage](#usage)
- [Credits / Resources](#credits--resources)
- [License](#license)
## Features
atom_string(const char *str): Interns a string and returns a pointer to the interned string. It is automatically converted to a string if given an argument of type const char *.<br>
atom_int(int64_t n): Converts an integer to its string representation and interns it. Useful for handling numeric strings efficiently.
## Usage
## Credits / Resources
[Tom Preston-Werner README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development)<br>
[Make a README](https://www.makeareadme.com/)<br>
[Choose a LICENSE](https://choosealicense.com/)<br>
[C Interfaces and Implementations](https://github.com/drh/cii)<br>
[Glib - Quark](https://docs.gtk.org/glib/alias.Quark.html)<br>
## License
This project is licensed under MIT - see the [LICENSE](LICENSE) file for details.

17
include/atom.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef ATOM_INCLUDED
#define ATOM_INCLUDED
#include <stdint.h>
// Return the length of a given atom.
extern int atom_len(const char *str);
// Create or retrieve an atom corresponding to a string with a given length
extern const char *atom_new (const char *str, int64_t len);
// Get the atom corresponding to a string
extern const char *atom_string(const char *str);
// Function to convert an integer to an atom (string representation of the number)
extern const char *atom_int(int64_t n);
#endif

105
include/except.h Normal file
View File

@@ -0,0 +1,105 @@
#ifndef EXCEPT_INCLUDED
#define EXCEPT_INCLUDED
#include <setjmp.h>
struct Exception {
const char *reason;
};
typedef struct Exception Exception;
struct Except_Frame {
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const struct Exception *exception;
};
typedef struct Except_Frame Except_Frame;
enum { EXCEPT_STATE_ENTERED=0,
EXCEPT_STATE_RAISED,
EXCEPT_STATE_HANDLED,
EXCEPT_STATE_FINALIZED,
EXCEPT_STATE_COUNT};
extern struct Except_Frame *except_stack;
extern const struct Exception assertion_failed;
void except_raise(const Exception *e, const char *file, int line);
#undef assert
#ifdef NDEBUG
#define assert(e) ((void)0)
#else
extern void asserted(int e);
#define assert(e) ((void)((e)||(RAISE(assertion_failed),0)))
#endif
#ifdef WIN32
#include <windows.h>
extern DWORD except_index;
extern void except_init(void);
extern void except_push(Except_Frame *fp);
extern void except_pop(void);
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
#define RERAISE except_raise(except_frame.exception, \
except_frame.file, except_frame.line)
#define RETURN switch (except_pop(),0) default: return
#define TRY do { \
volatile int except_flag; \
Except_Frame except_frame; \
if (except_index == TLS_OUT_OF_INDEXES) \
except_init(); \
except_push(&except_frame); \
except_flag = setjmp(except_frame.env); \
if (except_flag == EXCEPT_STATE_ENTERED) {
#define EXCEPT(e) \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} else if (except_frame.exception == &(e)) { \
except_flag = EXCEPT_STATE_HANDLED;
#define ELSE \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} else { \
except_flag = EXCEPT_STATE_HANDLED;
#define FINALLY \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} { \
if (except_flag == EXCEPT_STATE_ENTERED) \
except_flag = EXCEPT_STATE_FINALIZED;
#define END_TRY \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
} while (0)
#else
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
#define RERAISE except_raise(except_frame.exception, \
except_frame.file, except_frame.line)
#define RETURN switch (except_stack = except_stack->prev,0) default: return
#define TRY do { \
volatile int except_flag; \
Except_Frame except_frame; \
except_frame.prev = except_stack; \
except_stack = &except_frame; \
except_flag = setjmp(except_frame.env); \
if (except_flag == EXCEPT_STATE_ENTERED) {
#define EXCEPT(e) \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} else if (except_frame.exception == &(e)) { \
except_flag = EXCEPT_STATE_HANDLED;
#define ELSE \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} else { \
except_flag = EXCEPT_STATE_HANDLED;
#define FINALLY \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} { \
if (except_flag == EXCEPT_STATE_ENTERED) \
except_flag = EXCEPT_STATE_FINALIZED;
#define END_TRY \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
} while (0)
#endif
#endif // except.h

31
include/mem.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef MEM_INCLUDED
#define MEM_INCLUDED
#include "except.h" // Exceptions
#include <stddef.h> // size_t
/* General Macros*/
#define MEM_KB(x) ((size_t)(x) * 1024ULL)
#define MEM_MB(x) ((size_t)(x) * 1024ULL * 1024ULL)
#define MEM_GB(x) ((size_t)(x) * 1024ULL * 1024ULL * 1024ULL)
#define MEM_SIZE(x) (ptrdiff_t)sizeof(x)
#define MEM_COUNT(a) (MEM_SIZE(a) / MEM_SIZE(*(a)))
#define MEM_LEN(s) (MEM_COUNT(s) - 1)
extern const Exception oom; // Out of memory
extern void *mem_alloc (size_t nbytes,const char *file, int line);
extern void *mem_calloc(size_t count, size_t nbytes, const char *file, int line);
extern void mem_free(void *ptr, const char *file, int line);
extern void *mem_resize(void *ptr, size_t nbytes, const char *file, int line);
#define ALLOC(nbytes) mem_alloc((nbytes), __FILE__, __LINE__)
#define CALLOC(count, nbytes) mem_calloc((count), (nbytes), __FILE__, __LINE__)
#define NEW(p) ((p) = ALLOC((size_t)sizeof *(p)))
#define NEW0(p) ((p) = CALLOC(1, (size_t)sizeof *(p)))
#define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0))
#define RESIZE(ptr, nbytes) ((ptr) = mem_resize((ptr), (nbytes), __FILE__, __LINE__))
#endif

154
src/atom.c Normal file
View File

@@ -0,0 +1,154 @@
#include "../include/atom.h"
#include "../include/mem.h"
#include "../include/except.h"
#include <string.h>
#include <limits.h>
static struct Atom {
struct Atom *link; // Pointer to next atom (used for collisions in hash table)
int64_t len; // Length of the string
char *str; // The actual string data
} *buckets[2048]; // Array of 2048 atom "buckets" (used for the hash table)
// Predefined scatter values, used for generating hash values.
static uint32_t scatter[] = {
2078917053, 143302914, 1027100827, 1953210302, 755253631, 2002600785,
1405390230, 45248011, 1099951567, 433832350, 2018585307, 438263339,
813528929, 1703199216, 618906479, 573714703, 766270699, 275680090,
1510320440, 1583583926, 1723401032, 1965443329, 1098183682, 1636505764,
980071615, 1011597961, 643279273, 1315461275, 157584038, 1069844923,
471560540, 89017443, 1213147837, 1498661368, 2042227746, 1968401469,
1353778505, 1300134328, 2013649480, 306246424, 1733966678, 1884751139,
744509763, 400011959, 1440466707, 1363416242, 973726663, 59253759,
1639096332, 336563455, 1642837685, 1215013716, 154523136, 593537720,
704035832, 1134594751, 1605135681, 1347315106, 302572379, 1762719719,
269676381, 774132919, 1851737163, 1482824219, 125310639, 1746481261,
1303742040, 1479089144, 899131941, 1169907872, 1785335569, 485614972,
907175364, 382361684, 885626931, 200158423, 1745777927, 1859353594,
259412182, 1237390611, 48433401, 1902249868, 304920680, 202956538,
348303940, 1008956512, 1337551289, 1953439621, 208787970, 1640123668,
1568675693, 478464352, 266772940, 1272929208, 1961288571, 392083579,
871926821, 1117546963, 1871172724, 1771058762, 139971187, 1509024645,
109190086, 1047146551, 1891386329, 994817018, 1247304975, 1489680608,
706686964, 1506717157, 579587572, 755120366, 1261483377, 884508252,
958076904, 1609787317, 1893464764, 148144545, 1415743291, 2102252735,
1788268214, 836935336, 433233439, 2055041154, 2109864544, 247038362,
299641085, 834307717, 1364585325, 23330161, 457882831, 1504556512,
1532354806, 567072918, 404219416, 1276257488, 1561889936, 1651524391,
618454448, 121093252, 1010757900, 1198042020, 876213618, 124757630,
2082550272, 1834290522, 1734544947, 1828531389, 1982435068, 1002804590,
1783300476, 1623219634, 1839739926, 69050267, 1530777140, 1802120822,
316088629, 1830418225, 488944891, 1680673954, 1853748387, 946827723,
1037746818, 1238619545, 1513900641, 1441966234, 367393385, 928306929,
946006977, 985847834, 1049400181, 1956764878, 36406206, 1925613800,
2081522508, 2118956479, 1612420674, 1668583807, 1800004220, 1447372094,
523904750, 1435821048, 923108080, 216161028, 1504871315, 306401572,
2018281851, 1820959944, 2136819798, 359743094, 1354150250, 1843084537,
1306570817, 244413420, 934220434, 672987810, 1686379655, 1301613820,
1601294739, 484902984, 139978006, 503211273, 294184214, 176384212,
281341425, 228223074, 147857043, 1893762099, 1896806882, 1947861263,
1193650546, 273227984, 1236198663, 2116758626, 489389012, 593586330,
275676551, 360187215, 267062626, 265012701, 719930310, 1621212876,
2108097238, 2026501127, 1865626297, 894834024, 552005290, 1404522304,
48964196, 5816381, 1889425288, 188942202, 509027654, 36125855,
365326415, 790369079, 264348929, 513183458, 536647531, 13672163,
313561074, 1730298077, 286900147, 1549759737, 1699573055, 776289160,
2143346068, 1975249606, 1136476375, 262925046, 92778659, 1856406685,
1884137923, 53392249, 1735424165, 1602280572
};
// Function to get the atom corresponding to a string
const char *atom_string(const char *str) {
assert(str); // Ensure the string is not NULL
return atom_new(str, strlen(str)); // Call atom_new to retrieve the atom
}
// Function to convert an integer to an atom (string representation of the number)
const char *atom_int(int64_t n) {
char str[43];
char *s = str + sizeof str;
size_t m; // Used to hold the absolute value of the number
// Handle the special case of the minimum long value (LONG_MIN) which cannot be negated
if (n == LONG_MIN)
m = LONG_MAX + 1UL; // Adjust for LONG_MIN
else if (n < 0)
m = -n; // Take the absolute value if the number is negative
else
m = n; // Use the number directly if it is positive
// Convert the number to a string by repeatedly taking modulus 10 (for digits)
do
*--s = m % 10 + '0'; // Store the digit as a character
while ((m /= 10) > 0); // Divide by 10 until no digits are left
// If the number is negative, add the '-' sign at the front
if (n < 0)
*--s = '-';
// Return the atom corresponding to the string
return atom_new(s, (str + sizeof str) - s);
}
// Function to create or retrieve an atom corresponding to a string with a given length
const char *atom_new(const char *str, int64_t len) {
uint32_t h; // hash value of the string
int i;
struct Atom *p;
assert(str); // Ensure the string is not NULL
assert(len >= 0); // Ensure the length is non-negative
// Compute the hash value for the string using scatter.
for (h = 0, i = 0; i < len; i++)
h = (h << 1) + scatter[(unsigned char)str[i]]; // Use the scatter array for hash generation
h &= MEM_COUNT(buckets) - 1; // Modulo the hash by the number of buckets to ensure it fits
// Search for an existing atom with the same string and length (to avoid duplicates)
for (p = buckets[h]; p; p = p->link)
if (len == p->len) {
// If the atom has the same length, compare each character to check for equality
for (i = 0; i < len && p->str[i] == str[i]; )
i++;
// If the strings match, return the existing atom's string
if (i == len)
return p->str;
}
// If no matching atom was found, allocate a new one
p = ALLOC(sizeof (*p) + len + 1); // Allocate memory for the new atom
p->len = len; // Set the length of the string
p->str = (char *)(p + 1); // The string follows the atom structure in memory
// Copy the string data into the newly allocated atom (if the length is greater than 0)
if (len > 0)
memcpy(p->str, str, len);
p->str[len] = '\0'; // Null-terminate the string
// Insert the new atom into the corresponding hash bucket
p->link = buckets[h];
buckets[h] = p;
// Return the string of the new atom
return p->str;
}
// Function to get the length of an atom (string)
int atom_length(const char *str) {
struct Atom *p;
int i;
assert(str); // Ensure the string is not NULL
// Search through all the atom buckets to find the atom corresponding to the string
for (i = 0; i < MEM_COUNT(buckets); i++)
for (p = buckets[i]; p; p = p->link)
if (p->str == str) // If the string matches, return the length
return p->len;
// If the atom was not found, there's an error in the program
assert(0);
return 0; // This line is unreachable, just to avoid a compiler warning
}

82
src/except.c Normal file
View File

@@ -0,0 +1,82 @@
#include <stdlib.h>
#include <stdio.h>
#include "../include/except.h"
struct Except_Frame *except_stack = NULL;
const struct Exception assertion_failed = { "Assertion failed" };
void asserted(int e){
if(!e){
RAISE(assertion_failed);
}
}
void except_raise(const struct Exception *e, const char *file,int line)
{
#ifdef WIN32
Except_Frame *p;
if (except_index == TLS_OUT_OF_INDEXES)
except_init();
p = TlsGetValue(except_index);
#else
struct Except_Frame *p = except_stack;
#endif
asserted(e != NULL);
if (p == NULL) {
fprintf(stderr, "Uncaught exception");
if (e->reason)
fprintf(stderr, " %s", e->reason);
else
fprintf(stderr, " at 0x%p", (void*)e);
if (file && line > 0)
fprintf(stderr, " raised at %s:%d\n", file, line);
fprintf(stderr, "aborting...\n");
fflush(stderr);
abort();
}
p->exception = e;
p->file = file;
p->line = line;
#ifdef WIN32
except_pop();
#else
except_stack = except_stack->prev;
#endif
longjmp(p->env, EXCEPT_STATE_RAISED);
}
#ifdef WIN32
_CRTIMP void __cdecl _assert(void *, void *, unsigned);
#undef assert
#define assert(e) ((e) || (_assert(#e, __FILE__, __LINE__), 0))
DWORD except_index = -1;
void except_init(void) {
BOOL cond;
except_index = TlsAlloc();
assert(except_index != TLS_OUT_OF_INDEXES);
cond = TlsSetValue(except_index, NULL);
assert(cond == TRUE);
}
void except_push(Except_Frame *fp) {
BOOL cond;
fp->prev = TlsGetValue(except_index);
cond = TlsSetValue(except_index, fp);
assert(cond == TRUE);
}
void except_pop(void) {
BOOL cond;
Except_Frame *tos = TlsGetValue(except_index);
cond = TlsSetValue(except_index, tos->prev);
assert(cond == TRUE);
}
#endif

62
src/mem.c Normal file
View File

@@ -0,0 +1,62 @@
#include <stdlib.h>
#include <stddef.h>
#include "../include/except.h"
#include "../include/mem.h"
const struct Exception oom = { "Out of memory!" };
void *mem_alloc(size_t nbytes, const char *file, int line)
{
void *ptr;
assert(nbytes > 0);
ptr = malloc(nbytes);
if (ptr == NULL)
{
if (file == NULL)
RAISE(oom);
else
except_raise(&oom, file, line);
}
return ptr;
}
void *mem_calloc(size_t count, size_t nbytes, const char *file, int line)
{
void *ptr;
assert(count > 0);
assert(nbytes > 0);
ptr = calloc(count, nbytes);
if (ptr == NULL)
{
if (file == NULL)
RAISE(oom);
else
except_raise(&oom, file, line);
}
return ptr;
}
void mem_free(void *ptr, const char *file, int line)
{
(void) file;
(void) line;
if (ptr)
free(ptr);
}
void *mem_resize(void *ptr, size_t nbytes, const char *file, int line)
{
assert(ptr); // ?
assert(nbytes > 0);// ?
void *tmp = realloc(ptr, nbytes);
if (tmp == NULL)
{
if (file == NULL)
RAISE(oom);
else
except_raise(&oom, file, line);
}
ptr = tmp;
return ptr;
}

61
tests/01_atom.c Normal file
View File

@@ -0,0 +1,61 @@
#define DEBUG
#include "../include/atom.h"
#include <stdlib.h>
#include <stdio.h>
void test_positive_integers() {
int test_values[] = { 1, 42, 123456, 9999999 };
for (int i = 0; i < 4; i++) {
const char *result = atom_int(test_values[i]);
printf("Input: %d -> Result: %s\n", test_values[i], result);
}
}
void test_negative_integers() {
int test_values[] = { -1, -42, -123456, -9999999 };
for (int i = 0; i < 4; i++) {
const char *result = atom_int(test_values[i]);
printf("Input: %d -> Result: %s\n", test_values[i], result);
}
}
void test_int64_min() {
int64_t min_value = INT64_MIN;
const char *result = atom_int(min_value);
printf("Input: %ld -> Result: %s\n", min_value, result);
}
void test_zero() {
int zero_value = 0;
const char *result = atom_int(zero_value);
printf("Input: %d -> Result: %s\n", zero_value, result);
}
void test_large_numbers() {
int64_t large_positive = 9223372036854775807; // Largest 64-bit signed integer
int64_t large_negative = -9223372036854775807; // Negative version of the largest integer
const char *result_pos = atom_int(large_positive);
const char *result_neg = atom_int(large_negative);
printf("Input: %ld -> Result: %s\n", large_positive, result_pos);
printf("Input: %ld -> Result: %s\n", large_negative, result_neg);
}
int main(void){
printf("Testing Positive Integers:\n");
test_positive_integers();
printf("\nTesting Negative Integers:\n");
test_negative_integers();
printf("\nTesting INT64_MIN Edge Case:\n");
test_int64_min();
printf("\nTesting Zero Case:\n");
test_zero();
printf("\nTesting Large Numbers:\n");
test_large_numbers();
return EXIT_SUCCESS;
}