#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};
counting_release};
{
return EXIT_FAILURE;
}
printf("%lu x %lu points, %zu blocks of the library, %zu bytes at most\n",
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...
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