libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
custom_allocator.c
/* Track the memory use of the library with a custom allocator. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct usage
{
size_t current;
size_t peak;
size_t blocks;
} usage;
typedef union block_header
{
size_t size;
long long integer;
long double real;
void *pointer;
} block_header;
static void *counting_allocate(void *user, size_t size)
{
usage *memory = user;
block_header *block = malloc(sizeof *block + size);
if (block == NULL)
{
return NULL;
}
block->size = size;
memory->current += size;
memory->peak =
memory->current > memory->peak ? memory->current : memory->peak;
++memory->blocks;
return block + 1;
}
static void counting_release(void *user, void *data)
{
usage *memory = user;
block_header *block = (block_header *)data - 1;
memory->current -= block->size;
free(block);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "usage: %s input.sdf\n", argv[0]);
return EXIT_FAILURE;
}
usage memory = {0, 0, 0};
const sdfio_allocator allocator = {&memory, counting_allocate,
counting_release};
sdfio_reader *reader = NULL;
sdfio_reader_create(&allocator, &reader);
const sdfio_status status = sdfio_open_file(reader, argv[1]);
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
return EXIT_FAILURE;
}
const sdfio_header *header = sdfio_reader_header(reader);
printf("%lu x %lu points, %zu blocks of the library, %zu bytes at most\n",
(unsigned long)sdfio_header_columns(header),
(unsigned long)sdfio_header_rows(header), memory.blocks,
memory.peak);
printf("%zu bytes left after the reader is freed\n", memory.current);
return memory.current == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Files by path, and the stream over a C file.
sdfio_status sdfio_open_file(sdfio_reader *reader, const char *path)
Opens the file at a path as the source and reads its header, as sdfio_open_stream() does for a stream...
struct sdfio_header sdfio_header
Header of a file, with the field names of the standard.
Definition header.h:42
uint32_t sdfio_header_rows(const sdfio_header *header)
Returns the number of rows N of the data matrix: 1 for a profile.
uint32_t sdfio_header_columns(const sdfio_header *header)
Returns the number of columns M of the data matrix: the points along x.
sdfio_status sdfio_reader_add_binary(sdfio_reader *reader)
Adds the binary format to the formats that the reader reads, the version numbers bISO-1....
const sdfio_header * sdfio_reader_header(const sdfio_reader *reader)
Returns the header of the opened file, or null if no file is open.
struct sdfio_reader sdfio_reader
Opaque handle of a reader.
Definition reader.h:58
sdfio_status sdfio_reader_add_ascii(sdfio_reader *reader)
Adds the ASCII format to the formats that the reader reads, the version numbers aISO-1....
sdfio_status sdfio_reader_create(const sdfio_allocator *allocator, sdfio_reader **reader)
Creates a reader.
const char * sdfio_reader_error_message(const sdfio_reader *reader)
Returns a description of the most recent failure of the reader, or an empty string if none occurred.
void sdfio_reader_free(sdfio_reader *reader)
Releases a reader and closes the file that sdfio_open_file() opened, if any.
Reading and writing of ISO 25178-71 SDF surface data files.
Memory management callbacks that replace malloc() and free(), for example with a pool or a bump alloc...
Definition types.h:180
const char * sdfio_status_string(sdfio_status status)
Returns a constant description of a status.
sdfio_status
Result of a function call.
Definition types.h:31
@ SDFIO_OK
The call succeeded.
Definition types.h:33