commit 5b71d4ebf1adce3a0fa15b05e79b96f04f953aab
parent e08301d5925b11331bade915f5d2668e444c4835
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 4 Jun 2023 17:35:26 +0200
Use allocator interface and just build system
Diffstat:
9 files changed, 129 insertions(+), 44 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,4 @@
.cache
build
+build.jst
compile_commands.json
diff --git a/.gitmodules b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "lib/liballoc"]
+ path = lib/liballoc
+ url = git@sinitax.com:sinitax/liballoc
diff --git a/Makefile b/Makefile
@@ -30,7 +30,7 @@ include $(DEPS)
build/libidx.a: $(OBJS) | build
$(CC) -o build/tmp.o $^ $(CFLAGS) -r
- objcopy --keep-global-symbols=libidx.abi build/tmp.o build/fixed.o
+ objcopy --keep-global-symbols=libidx.api build/tmp.o build/fixed.o
ar rcs $@ build/fixed.o
build/libidx.so: $(PI_OBJS) | build
diff --git a/build.jst.tmpl b/build.jst.tmpl
@@ -0,0 +1,66 @@
+#default PREFIX /usr/local
+#default INCLDIR /include
+#default LIBDIR /lib
+#default CC gcc
+
+#ifdef LIBIDX_DEBUG
+#define DEBUG 1
+#endif
+
+#ifdef DEBUG
+#define OPT_CFLAGS -Og -g
+#else
+#define OPT_CFLAGS -O2
+#endif
+
+cflags = -std=c99 -Wunused-variable -Wunused-function -Wconversion -Wformat
+ -I include -I lib/liballoc/include
+ #{OPT_CFLAGS} #{LIBIDX_EXTRA_CFLAGS} #{EXTRA_CFLAGS}
+
+rule liba
+ gcc -o $out.tmp.o $in $cflags -r
+ objcopy --keep-global-symbols=libidx.api $out.tmp.o $out.fixed.o
+ ar rcs $out $out.fixed.o
+ rm $out.tmp.o $out.fixed.o
+
+rule libso
+ gcc -o $out $in $cflags -shared -Wl,-version-script libidx.lds
+
+rule cc
+ gcc -o $out $in $cflags
+
+rule mkdir
+ mkdir $out
+
+target build
+ mkdir
+
+target build/libidx.a
+ liba src/idx.c | include/idx.h build
+
+target build/libidx.so
+ liba src/idx.c | include/idx.h build
+
+target build/test
+ cc src/test.c build/libidx.a | build
+
+command clean
+ rm -rf build
+
+command cleanall
+ just clean
+ just -C lib/liballoc cleanall
+
+command install
+ install -m755 include/idx.h -t "#{DESTDIR}#{PREFIX}#{INCLDIR}"
+ install -m755 build/libidx.a -t "#{DESTDIR}#{PREFIX}#{LIBDIR}"
+ install -m755 build/libidx.so -t "#{DESTDIR}#{PREFIX}#{LIBDIR}"
+
+command uninstall
+ rm -f "#{DESTDIR}#{PREFIX}#{INCLDIR}/idx.h"
+ rm -f "#{DESTDIR}#{PREFIX}#{LIBDIR}/libidx.a"
+ rm -f "#{DESTDIR}#{PREFIX}#{LIBDIR}/libidx.so"
+
+command all
+ just build/libidx.a build/libidx.so
+
diff --git a/configure b/configure
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+tmpl "$@" build.jst.tmpl > build.jst
+for lib in ./lib/*; do
+ pushd $lib
+ ./configure "$@"
+ popd
+done
diff --git a/include/idx.h b/include/idx.h
@@ -1,7 +1,9 @@
#pragma once
+#include "allocator.h"
+
#include <stdint.h>
-#include <stdlib.h>
+#include <stddef.h>
enum {
IDX_TYPE_UNKNOWN = -1,
@@ -27,7 +29,6 @@ enum {
IDX_FILE_READ_MAGIC,
IDX_FILE_READ_DIMS,
IDX_FILE_READ_DATA,
- IDX_OUT_OF_MEMORY,
};
struct idx {
@@ -45,16 +46,20 @@ struct idx {
uint8_t count;
uint32_t *sizes;
} dims;
+
+ const struct allocator *allocator;
};
-int idx_new(struct idx *idx, uint8_t dims_count, uint32_t *dims, int data_type);
+int idx_new(struct idx *idx, uint8_t dims_count, uint32_t *dims,
+ int data_type, const struct allocator *allocator);
int idx_wrap(struct idx *idx, void *data, uint8_t dims_count,
uint32_t *dims, int data_type);
-int idx_load(struct idx *idx, const char *filename);
+int idx_load(struct idx *idx, const char *filename,
+ const struct allocator *allocator);
int idx_save(struct idx *idx, const char *filename);
-void idx_deinit(struct idx *idx);
+int idx_deinit(struct idx *idx);
void *idx_get(struct idx *idx, int dims, ...);
diff --git a/lib/liballoc b/lib/liballoc
@@ -0,0 +1 @@
+Subproject commit 3f388a2659ae2d121322101930d33412815d84e6
diff --git a/libidx.abi b/libidx.api
diff --git a/src/idx.c b/src/idx.c
@@ -1,14 +1,14 @@
+#define _DEFAULT_SOURCE
+
#include "idx.h"
+#include <endian.h>
#include <stdbool.h>
-#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
-#define ASSERT(x) assert((x), __FILE__, __LINE__, #x)
-
-static const int idx_type_encode_lut[IDX_TYPE_COUNT] = {
+static const uint8_t idx_type_encode_lut[IDX_TYPE_COUNT] = {
[IDX_TYPE_UNSIGNED_BYTE] = 0x08,
[IDX_TYPE_SIGNED_BYTE] = 0x09,
[IDX_TYPE_SHORT] = 0x0B,
@@ -17,7 +17,7 @@ static const int idx_type_encode_lut[IDX_TYPE_COUNT] = {
[IDX_TYPE_DOUBLE] = 0x0E,
};
-static const int idx_type_size[IDX_TYPE_COUNT] = {
+static const uint8_t idx_type_size[IDX_TYPE_COUNT] = {
[IDX_TYPE_UNSIGNED_BYTE] = 1,
[IDX_TYPE_SIGNED_BYTE] = 1,
[IDX_TYPE_SHORT] = 2,
@@ -26,9 +26,6 @@ static const int idx_type_size[IDX_TYPE_COUNT] = {
[IDX_TYPE_DOUBLE] = 8
};
-static void assert(bool cond, const char *file,
- size_t line, const char *condstr);
-
static bool valid_idx_type(int type);
static int idx_type_decode(int val);
@@ -36,17 +33,6 @@ static int idx_type_decode(int val);
static bool fread_u32(FILE *f, uint32_t *val);
static bool fwrite_u32(FILE *f, uint32_t val);
-void
-assert(bool cond, const char *file, size_t line, const char *condstr)
-{
- if (cond) return;
-
- fprintf(stderr, "LIBIDX: Assertion failed at %s:%li (%s)\n",
- file, line, condstr);
-
- exit(1);
-}
-
bool
valid_idx_type(int type)
{
@@ -69,7 +55,7 @@ idx_type_decode(int val)
bool
fread_u32(FILE *f, uint32_t *val)
{
- int len;
+ size_t len;
len = fread(val, 4, 1, f);
if (len != 1) return false;
@@ -81,7 +67,7 @@ fread_u32(FILE *f, uint32_t *val)
bool
fwrite_u32(FILE *f, uint32_t val)
{
- int len;
+ size_t len;
val = htobe32(val);
len = fwrite(&val, 4, 1, f);
@@ -91,9 +77,11 @@ fwrite_u32(FILE *f, uint32_t val)
}
int
-idx_new(struct idx *idx, uint8_t dims_count, uint32_t *dims, int data_type)
+idx_new(struct idx *idx, uint8_t dims_count, uint32_t *dims, int data_type,
+ const struct allocator *allocator)
{
size_t i, size;
+ int rc;
if (!valid_idx_type(data_type))
return IDX_BAD_PARAM;
@@ -102,9 +90,9 @@ idx_new(struct idx *idx, uint8_t dims_count, uint32_t *dims, int data_type)
for (i = 0; i < dims_count; i++)
size *= dims[i];
- idx->buf.data = malloc(size);
- if (idx->buf.data == NULL)
- return IDX_OUT_OF_MEMORY;
+ idx->allocator = allocator;
+ idx->buf.data = allocator->alloc(allocator, size, &rc);
+ if (!idx->buf.data) return -rc;
idx->buf.len = size;
idx->dims.sizes = dims; /* move */
@@ -129,6 +117,8 @@ idx_wrap(struct idx *idx, void *data, uint8_t dims_count,
for (i = 0; i < dims_count; i++)
size *= dims[i];
+ idx->allocator = NULL;
+
idx->buf.data = data; /* move */
idx->buf.len = size;
@@ -142,11 +132,13 @@ idx_wrap(struct idx *idx, void *data, uint8_t dims_count,
}
int
-idx_load(struct idx *idx, const char *filename)
+idx_load(struct idx *idx, const char *filename,
+ const struct allocator *allocator)
{
uint32_t magic;
- int len, i;
+ size_t len, i;
FILE *file;
+ int rc;
file = fopen(filename, "r");
if (file == NULL)
@@ -168,9 +160,10 @@ idx_load(struct idx *idx, const char *filename)
if (idx->dims.count == 0)
return IDX_FILE_BAD_DIMS;
- idx->dims.sizes = malloc(sizeof(uint32_t) * idx->dims.count);
- if (idx->dims.sizes == NULL)
- return IDX_OUT_OF_MEMORY;
+ idx->allocator = allocator;
+ idx->dims.sizes = allocator->alloc(allocator,
+ sizeof(uint32_t) * idx->dims.count, &rc);
+ if (!idx->dims.sizes) return -rc;
/* read dims */
idx->buf.len = 1;
@@ -180,9 +173,8 @@ idx_load(struct idx *idx, const char *filename)
idx->buf.len *= idx->dims.sizes[i];
}
- idx->buf.data = malloc(idx->buf.len);
- if (idx->buf.data == NULL)
- return IDX_OUT_OF_MEMORY;
+ idx->buf.data = allocator->alloc(allocator, idx->buf.len, &rc);
+ if (idx->buf.data) return -rc;
/* read data */
len = fread(idx->buf.data, idx->type.size, idx->buf.len, file);
@@ -232,11 +224,20 @@ idx_save(struct idx *idx, const char *filename)
return IDX_OK;
}
-void
+int
idx_deinit(struct idx *idx)
{
- free(idx->buf.data);
- free(idx->dims.sizes);
+ int rc;
+
+ if (!idx->allocator) return IDX_OK;
+
+ rc = idx->allocator->free(idx->allocator, idx->buf.data);
+ if (rc) return -rc;
+
+ rc = idx->allocator->free(idx->allocator, idx->dims.sizes);
+ if (rc) return -rc;
+
+ return IDX_OK;
}
void *
@@ -244,7 +245,7 @@ idx_get(struct idx *idx, int dims, ...)
{
va_list ap;
size_t stride;
- int i, val;
+ size_t i, val;
void *p;
if (dims < 0 || dims > idx->dims.count)
@@ -257,7 +258,7 @@ idx_get(struct idx *idx, int dims, ...)
p = idx->buf.data;
va_start(ap, dims);
for (i = 0; i < dims; i++) {
- val = va_arg(ap, int);
+ val = va_arg(ap, size_t);
if (val >= idx->dims.sizes[i])
return NULL;