Skip to content

Commit

Permalink
Adds additional types
Browse files Browse the repository at this point in the history
  • Loading branch information
simondevenish committed Nov 19, 2024
1 parent bb62d45 commit ac5e737
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/allocx/allocx_assertions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef ALLOCX_ASSERTIONS_H
#define ALLOCX_ASSERTIONS_H

#include <assert.h>
#include <stdio.h>

#define ALLOCX_ASSERT(condition, message) \
do { \
if (!(condition)) { \
fprintf(stderr, "Assertion failed: %s\n", message); \
assert(condition); \
} \
} while (0)

#endif // ALLOCX_ASSERTIONS_H
30 changes: 30 additions & 0 deletions include/allocx/allocx_atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef ALLOCX_ATOMIC_H
#define ALLOCX_ATOMIC_H

#include <stdatomic.h>

// Memory order macros
#define ALLOCX_MEMORY_ORDER_RELAXED memory_order_relaxed
#define ALLOCX_MEMORY_ORDER_ACQUIRE memory_order_acquire
#define ALLOCX_MEMORY_ORDER_RELEASE memory_order_release
#define ALLOCX_MEMORY_ORDER_SEQ_CST memory_order_seq_cst

// Memory Barrier Macro
#define ALLOCX_MEMORY_BARRIER() __sync_synchronize()

// Atomic Operations
#define ALLOCX_ACQUIRE(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE)
#define ALLOCX_RELEASE(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_RELEASE)
#define ALLOCX_RELAXED(ptr) __atomic_load_n(ptr, __ATOMIC_RELAXED)
#define ALLOCX_SEQ_CST(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_SEQ_CST)
#define ALLOCX_ATOMIC_EXCHANGE(ptr, new_value) __atomic_exchange_n(ptr, new_value, __ATOMIC_SEQ_CST)
#define ALLOCX_CAS(ptr, expected, desired) __atomic_compare_exchange_n( \
ptr, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)

// Atomic Operations for Lock-Free
#define ALLOCX_FETCH_ADD(ptr, value) __atomic_fetch_add(ptr, value, __ATOMIC_SEQ_CST)
#define ALLOCX_FETCH_SUB(ptr, value) __atomic_fetch_sub(ptr, value, __ATOMIC_SEQ_CST)
#define ALLOCX_FETCH_AND(ptr, value) __atomic_fetch_and(ptr, value, __ATOMIC_SEQ_CST)
#define ALLOCX_FETCH_OR(ptr, value) __atomic_fetch_or(ptr, value, __ATOMIC_SEQ_CST)

#endif // ALLOCX_ATOMIC_H
13 changes: 13 additions & 0 deletions include/allocx/allocx_errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef ALLOCX_ERRORS_H
#define ALLOCX_ERRORS_H

typedef enum {
ALLOCX_SUCCESS = 0,
ALLOCX_ERROR_OUT_OF_MEMORY,
ALLOCX_ERROR_INVALID_ARGUMENT,
ALLOCX_ERROR_ALIGNMENT,
ALLOCX_ERROR_OVERFLOW,
ALLOCX_ERROR_UNKNOWN
} allocx_error_t;

#endif // ALLOCX_ERRORS_H

0 comments on commit ac5e737

Please sign in to comment.