extracted prick into their own header files and deleted lib

This commit is contained in:
2026-03-10 21:08:24 +00:00
parent f811665ae9
commit 24f4962d8c
8 changed files with 250 additions and 236 deletions

View File

@@ -1,15 +1,11 @@
/* prick_aliases.h:
* Created: 2025-04-09
/* base.h: Basic definitions
* Created: 2026-03-10
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
This library defines some useful aliases for common types. These are mostly
handpicked, and aren't necessary.
*/
#ifndef PRICK_ALIASES_H
#define PRICK_ALIASES_H
#ifndef BASE_H
#define BASE_H
#include <assert.h>
#include <stdint.h>
@@ -29,16 +25,22 @@ static_assert(sizeof(double) == 8, "f64 requires 8 byte doubles");
typedef float f32;
typedef double f64;
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define SAFE_SUB(A, B) ((A) < (B) ? 0 : (A) - (B))
// 64 byte programs
#define SIZEOF_PROGRAM (1LU << 6)
#endif
/* Copyright (C) 2025 Aryadev Chavali
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the Unlicense for details.
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
* details.
* You may distribute and modify this code under the terms of the Unlicense,
* which you should have received a copy of along with this program. If not,
* please go to <https://unlicense.org/>.
* You may distribute and modify this code under the terms of the GNU General
* Public License Version 2, which you should have received a copy of along with
* this program. If not, please go to <https://www.gnu.org/licenses/>.
*/

View File

@@ -2,7 +2,7 @@
CFLAGS="-Wall -Wextra -Wpedantic -Wswitch-enum -Werror -std=c23 -ggdb"
LDFLAGS="-lm -lraylib"
SRC="main.c"
SRC="sv.c vec.c main.c"
OUT="main.out"
set -xe

View File

@@ -1,163 +0,0 @@
/* prick_vec.h: A dynamically sized array with SBO.
* Created: 2026-01-22
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
To utilise this library, please put:
#define PRICK_VEC_IMPL
#include "prick_vec.h"
in one of your code units.
This library defines another form of dynamically sized array as opposed to
prick_darr.h. This one is closer to the one classically implemented by most; a
structure with some metadata and a pointer to the raw buffer. This way,
pointers to the dynamic array are stable (as the structure itself is never
reallocated) and the array can still grow as required.
We use a trick, called Small Buffer Optimisation (SBO), to inline elements
directly into the structure when there are a small number of them (see
PRICK_VEC_INLINE_CAPACITY). This makes lookup _even faster_ (no derefence and
possibility of the entire vector existing in the CPU cache) and allows us to
avoid allocation for smaller use cases. If the number of elements exceeds
PRICK_VEC_INLINE_CAPACITY, we utilise the allocator.
*/
#ifndef PRICK_VEC_H
#define PRICK_VEC_H
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#define VEC_INLINE_CAPACITY 32
#define VEC_MULT 2
typedef struct
{
uint64_t size, capacity;
uint8_t not_inlined;
union
{
void *ptr;
alignas(max_align_t) uint8_t inlined[VEC_INLINE_CAPACITY];
};
} vec_t;
static_assert(sizeof(vec_t) == 64, "Expected sizeof(vec_t) to be 64");
void vec_append(vec_t *vec, const void *const ptr, uint64_t size);
void vec_append_byte(vec_t *vec, uint8_t byte);
void *vec_data(vec_t *vec);
void vec_ensure_capacity(vec_t *vec, uint64_t capacity);
void vec_ensure_free(vec_t *vec, uint64_t size);
void vec_free(vec_t *vec);
void vec_clone(vec_t *v2, vec_t *v1);
#define VEC_GET(VEC, INDEX, TYPE) (((TYPE *)vec_data(VEC))[INDEX])
#ifdef VEC_IMPL
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#include <stdlib.h>
#include <string.h>
void vec_append(vec_t *vec, const void *const ptr, uint64_t size)
{
if (!vec || !ptr || !size)
return;
vec_ensure_free(vec, size);
memcpy(&VEC_GET(vec, vec->size, uint8_t), ptr, size);
vec->size += size;
}
void vec_append_byte(vec_t *vec, uint8_t byte)
{
if (!vec)
return;
vec_ensure_free(vec, 1);
VEC_GET(vec, vec->size, uint8_t) = byte;
++vec->size;
}
void *vec_data(vec_t *vec)
{
if (!vec)
return NULL;
if (vec->not_inlined)
{
return vec->ptr;
}
else
{
return vec->inlined;
}
}
void vec_ensure_capacity(vec_t *vec, uint64_t capacity)
{
if (!vec)
return;
if (vec->capacity == 0)
vec->capacity = VEC_INLINE_CAPACITY;
if (vec->capacity < capacity)
{
vec->capacity = MAX(vec->capacity * VEC_MULT, capacity);
if (!vec->not_inlined)
{
// We were a small buffer, and now we cannot be i.e. we need to allocate
// on the heap.
vec->not_inlined = 1;
void *buffer = calloc(1, vec->capacity);
memcpy(buffer, vec->inlined, vec->size);
memset(vec->inlined, 0, sizeof(vec->inlined));
vec->ptr = buffer;
}
else
{
// We're already on the heap, just reallocate.
vec->ptr = realloc(vec->ptr, vec->capacity);
}
}
}
void vec_ensure_free(vec_t *vec, uint64_t size)
{
if (!vec)
return;
vec_ensure_capacity(vec, vec->size + size);
}
void vec_free(vec_t *vec)
{
if (!vec)
return;
if (vec->not_inlined)
free(vec->ptr);
memset(vec, 1, sizeof(*vec));
}
void vec_clone(vec_t *v2, vec_t *v1)
{
if (!v1 || !v2)
return;
vec_append(v2, vec_data(v1), v1->size);
}
#undef MAX
#endif
#endif
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the Unlicense for details.
* You may distribute and modify this code under the terms of the Unlicense,
* which you should have received a copy of along with this program. If not,
* please go to <https://unlicense.org/>.
*/

13
main.c
View File

@@ -10,17 +10,10 @@
#include <string.h>
#include <time.h>
#include "./lib/prick_aliases.h"
#include "base.h"
#include "sv.h"
#include "vec.h"
#define SV_IMPL
#include "./lib/prick_sv.h"
#define VEC_IMPL
#include "./lib/prick_vec.h"
#define SAFE_SUB(A, B) ((A) < (B) ? 0 : (A) - (B))
#define SIZEOF_PROGRAM (1LU << 6)
struct ProgramConcat
{
sv_t A, B;

View File

@@ -1,49 +1,16 @@
/* sv.h: String Views.
* Created: 2026-03-01
/* sv.c: String View implementation
* Created: 2026-03-10
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
To utilise this library, please put:
#define SV_IMPL
#include "sv.h"
in one of your code units.
This is a simple read-only string view library. It defines some extremely
common functions you'd expect for a string view library, excluding any that
require allocation.
Taken from https://github.com/oreodave/prick - prick_sv.
*/
#ifndef SV_H
#define SV_H
#include <stdint.h>
typedef struct
{
uint64_t size;
const char *data;
} sv_t;
#define SV(DATA, SIZE) ((sv_t){.data = (DATA), .size = (SIZE)})
#define SV_AUTO(DATA) ((sv_t){.data = (void *)(DATA), .size = sizeof(DATA) - 1})
// Pretty printers
#define SV_FMT(SV) (int)(SV).size, (SV).data
#define PR_SV "%.*s"
sv_t sv_chop_left(sv_t, uint64_t size);
sv_t sv_chop_right(sv_t, uint64_t size);
sv_t sv_truncate(sv_t, uint64_t newsize);
sv_t sv_substr(sv_t, uint64_t position, uint64_t size);
sv_t sv_till(sv_t, const char *reject);
sv_t sv_while(sv_t, const char *accept);
#ifdef SV_IMPL
#include <stddef.h>
#include <string.h>
#include "sv.h"
sv_t sv_chop_left(sv_t sv, uint64_t size)
{
if (sv.size <= size)
@@ -97,18 +64,17 @@ sv_t sv_while(sv_t sv, const char *accept)
return sv_truncate(sv, offset);
}
#endif
#endif
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the Unlicense for details.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License Version 2
* for details.
* You may distribute and modify this code under the terms of the Unlicense,
* which you should have received a copy of along with this program. If not,
* please go to <https://unlicense.org/>.
* You may distribute and modify this code under the terms of the
* GNU General Public License Version 2, which you should have received a copy
of along with this
* program. If not, please go to <https://www.gnu.org/licenses/>.
*/

50
sv.h Normal file
View File

@@ -0,0 +1,50 @@
/* sv.h: String Views
* Created: 2026-03-10
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
Taken from https://github.com/oreodave/prick - prick_sv.
*/
#ifndef SV_H
#define SV_H
#include <stdint.h>
typedef struct
{
uint64_t size;
const char *data;
} sv_t;
#define SV(DATA, SIZE) ((sv_t){.data = (DATA), .size = (SIZE)})
#define SV_AUTO(DATA) ((sv_t){.data = (void *)(DATA), .size = sizeof(DATA) - 1})
// Pretty printers
#define SV_FMT(SV) (int)(SV).size, (SV).data
#define PR_SV "%.*s"
sv_t sv_chop_left(sv_t, uint64_t size);
sv_t sv_chop_right(sv_t, uint64_t size);
sv_t sv_truncate(sv_t, uint64_t newsize);
sv_t sv_substr(sv_t, uint64_t position, uint64_t size);
sv_t sv_till(sv_t, const char *reject);
sv_t sv_while(sv_t, const char *accept);
#endif
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License Version 2
* for details.
* You may distribute and modify this code under the terms of the
* GNU General Public License Version 2, which you should have received a copy
of along with this
* program. If not, please go to <https://www.gnu.org/licenses/>.
*/

111
vec.c Normal file
View File

@@ -0,0 +1,111 @@
/* vec.c: Vector implementation
* Created: 2026-03-10
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
Taken from https://github.com/oreodave/prick - prick_vec.
*/
#include <stdlib.h>
#include <string.h>
#include "base.h"
#include "vec.h"
void vec_append(vec_t *vec, const void *const ptr, uint64_t size)
{
if (!vec || !ptr || !size)
return;
vec_ensure_free(vec, size);
memcpy(&VEC_GET(vec, vec->size, uint8_t), ptr, size);
vec->size += size;
}
void vec_append_byte(vec_t *vec, uint8_t byte)
{
if (!vec)
return;
vec_ensure_free(vec, 1);
VEC_GET(vec, vec->size, uint8_t) = byte;
++vec->size;
}
void *vec_data(vec_t *vec)
{
if (!vec)
return NULL;
if (vec->not_inlined)
{
return vec->ptr;
}
else
{
return vec->inlined;
}
}
void vec_ensure_capacity(vec_t *vec, uint64_t capacity)
{
if (!vec)
return;
if (vec->capacity == 0)
vec->capacity = VEC_INLINE_CAPACITY;
if (vec->capacity < capacity)
{
vec->capacity = MAX(vec->capacity * VEC_MULT, capacity);
if (!vec->not_inlined)
{
// We were a small buffer, and now we cannot be i.e. we need to allocate
// on the heap.
vec->not_inlined = 1;
void *buffer = calloc(1, vec->capacity);
memcpy(buffer, vec->inlined, vec->size);
memset(vec->inlined, 0, sizeof(vec->inlined));
vec->ptr = buffer;
}
else
{
// We're already on the heap, just reallocate.
vec->ptr = realloc(vec->ptr, vec->capacity);
}
}
}
void vec_ensure_free(vec_t *vec, uint64_t size)
{
if (!vec)
return;
vec_ensure_capacity(vec, vec->size + size);
}
void vec_free(vec_t *vec)
{
if (!vec)
return;
if (vec->not_inlined)
free(vec->ptr);
memset(vec, 1, sizeof(*vec));
}
void vec_clone(vec_t *v2, vec_t *v1)
{
if (!v1 || !v2)
return;
vec_append(v2, vec_data(v1), v1->size);
}
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License Version 2
* for details.
* You may distribute and modify this code under the terms of the
* GNU General Public License Version 2, which you should have received a copy
of along with this
* program. If not, please go to <https://www.gnu.org/licenses/>.
*/

55
vec.h Normal file
View File

@@ -0,0 +1,55 @@
/* vec.h: Vectors
* Created: 2026-03-10
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
Taken from https://github.com/oreodave/prick - prick_vec.
*/
#ifndef VEC_H
#define VEC_H
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#define VEC_INLINE_CAPACITY 32
#define VEC_MULT 2
typedef struct
{
uint64_t size, capacity;
uint8_t not_inlined;
union
{
void *ptr;
alignas(max_align_t) uint8_t inlined[VEC_INLINE_CAPACITY];
};
} vec_t;
static_assert(sizeof(vec_t) == 64, "Expected sizeof(vec_t) to be 64");
void vec_append(vec_t *vec, const void *const ptr, uint64_t size);
void vec_append_byte(vec_t *vec, uint8_t byte);
void *vec_data(vec_t *vec);
void vec_ensure_capacity(vec_t *vec, uint64_t capacity);
void vec_ensure_free(vec_t *vec, uint64_t size);
void vec_free(vec_t *vec);
void vec_clone(vec_t *v2, vec_t *v1);
#define VEC_GET(VEC, INDEX, TYPE) (((TYPE *)vec_data(VEC))[INDEX])
#endif
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
* details.
* You may distribute and modify this code under the terms of the GNU General
* Public License Version 2, which you should have received a copy of along with
* this program. If not, please go to <https://www.gnu.org/licenses/>.
*/