Libstephen Base

Libstephen: Base Declarations.

Author
Stephen Brennan
Date
Created Sunday, 3 August 2014
Copyright
Copyright (c) 2013-2016, Stephen Brennan. Released under the Revised BSD License. See the LICENSE.txt file for details.

Defines

smb_new(type, n)

A nicer allocation function.

This macro/function wraps malloc(). It’s inspired by the g_new function in glib. It allows you to allocate memory and adjust the allocation counter in one call. Instead of specifying the size of the memory, you specify type and number of instances.

Return
A pointer to the allocated memory, casted to the correct type.
Parameters
  • type: The type of the memory to allocate.
  • n: The number of instances to allocate.

smb_renew(type, ptr, newamt)

A nicer reallocation function.

This macro/function wraps realloc(). It allows you reallocate memory and adjust the allocation counter accordingly. Exits on failure.

Return
A pointer to the reallocated memory.
Parameters
  • type: The type of the memory to reallocate.
  • ptr: The memory to reallocate.
  • newamt: The amount of items to allocate.

smb_free(ptr)

A nicer free function.

This macro/function wraps free(). It exits on failure and adjusts the malloc counter.

Parameters
  • ptr: The memory you’re freeing.

SMB_SUCCESS
SMB_INDEX_ERROR
SMB_NOT_FOUND_ERROR
SMB_STOP_ITERATION
SMB_EXTERNAL_EXCEPTION_START
PRINT_ERROR_LOC
FLAG_CHECK(var, bit)
FLAG_SET(var, bit)
FLAG_CLEAR(var, bit)
LLINT(value)
PTR(value)
DBL(value)

Typedefs

typedef DATA

Base data type for the data structures.

Capable of containing long integers, double precision floats, or pointers. Takes up 8 bytes.

typedef (* DATA_ACTION)(DATA)

A function pointer that takes a DATA and performs an action on it.

The function could count it, call free on it, print it, etc. Useful for stuff like deleting data structures full of items (if they’re pointers to dynamically allocated data, they’ll need to be freed), applying an action to every item in a list (e.g. printing), and many more applications.

typedef (* DATA_COMPARE)(DATA, DATA)

A function pointer that takes two DATA and compares them.

This comparator function is used for two purposes: (1) to check for equality, and (2) to order data. If a particular type of data has no ordering, then it is sufficient for the purposes of equality testing to return 0 if equal, and 1 if not equal. However, this will fail for any function that uses the DATA_COMPARE to order DATA. Therefore, any function that takes a DATA_COMPARE should specify in its documentation whether it will use it for equality testing, or for ordering.

The DATA_COMPARE function shall return 0 iff the two DATA are equal. It shall return a value less than 0 iff the first is less than the second. It shall return a value greater than zero iff the first is greater than the second.

typedef (* DATA_PRINTER)(FILE *, DATA)

A function pointer that takes a DATA and prints it.

This function should not print a trailing newline! There are higher level functions that will deal with that.

typedef smb_status

An enumeration of all possible status values for libstephen functions.

Functions

int data_compare_string(DATA d1, DATA d2)
int data_compare_int(DATA d1, DATA d2)
int data_compare_float(DATA d1, DATA d2)
int data_compare_pointer(DATA d1, DATA d2)
void data_printer_string(FILE * f, DATA d)
void data_printer_int(FILE * f, DATA d)
void data_printer_float(FILE * f, DATA d)
void data_printer_pointer(FILE * f, DATA d)
void* smb___new(size_t amt)
void* smb___renew(void * ptr, size_t newsize)
void smb___free(void * ptr)
char* smb_status_string(smb_status status)
union DATA
#include <base.h>

Base data type for the data structures.

Capable of containing long integers, double precision floats, or pointers. Takes up 8 bytes.

Public Members

long long int data_llint
double data_dbl
void* data_ptr