Sat Sep 22 23:04:50 EDT 2001

2 libmba

The mba library consists of general functionality common to potentially any application.

2.1 stack

A dynamically resizing stack.


struct stack;

struct stack *stack_new(unsigned int max_size);
void stack_del(struct stack *s, void (*free_data_fn)(void *));

void stack_clear(struct stack *s, void (*free_data_fn)(void *));
int stack_push(struct stack *s, void *data);
void *stack_pop(struct stack *s);
int stack_is_empty(const struct stack *s);
unsigned int stack_size(const struct stack *s);
void stack_iterate(struct stack *s);
void *stack_next(struct stack *s);
void *stack_peek(struct stack *s);

2.2 linkedlist

A singularly linked list.


struct linkedlist;

struct linkedlist *linkedlist_new(unsigned int);
void linkedlist_del(struct linkedlist *l, void (*free_data_fn)(void *));

void linkedlist_clear(struct linkedlist *l, void (*free_data_fn)(void *));
int linkedlist_add(struct linkedlist *l, void *data);
int linkedlist_insert(struct linkedlist *l, unsigned int idx, void *data);
int linkedlist_is_empty(const struct linkedlist *l);
unsigned int linkedlist_size(const struct linkedlist *l);
void *linkedlist_get(const struct linkedlist *l, unsigned int idx);
void *linkedlist_get_last(const struct linkedlist *l);
void linkedlist_iterate(struct linkedlist *l);
void *linkedlist_next(struct linkedlist *l);
void *linkedlist_remove(struct linkedlist *l, unsigned int idx);
void *linkedlist_remove_last(struct linkedlist *l);

2.3 hashtable

A chaining hashtable.


#define HASHTABLE_SMALL 17
#define HASHTABLE_MEDIUM 701
#define HASHTABLE_LARGE 8191
#define HASHTABLE_XL 65521

struct hashtable;

unsigned int hash_string(const void *);

struct hashtable *hashtable_new(unsigned int size,
                                unsigned int (*hash_fn)(const void *),
                                void (*free_key_fn)(void *),
                                void (*free_data_fn)(void *));
void hashtable_del(struct hashtable *h);

void hashtable_clear(struct hashtable *h);
int hashtable_put(struct hashtable *h, void *key, void *data);
int hashtable_is_empty(struct hashtable *h);
unsigned int hashtable_size(struct hashtable *h);
void *hashtable_get(const struct hashtable *h, const void *key);
void hashtable_iterate(struct hashtable *h);
void *hashtable_next(struct hashtable *h);
void *hashtable_remove(struct hashtable *h, const void *key);

2.4 hexdump

Print a region of memory to a stream as hexadecimal characters.


void hexdump(FILE *stream, const void *src, unsigned int off,
                                unsigned int len, unsigned int width);

2.5 path

Manipulate UNIX file system paths.


struct path;
struct path *path_new(const char *s);
struct path *path_child_new(const struct path *parent, const char *child);
void path_del(struct path *p);

char *path_get(const struct path *p);
char *path_get_canonical(struct path *p);
int path_compare(const struct path *p1, const struct path *p2);
char *path_get_absolute(const struct path *p);
char *path_get_name(const struct path *p);
char *path_get_parent(const struct path *p);

2.6 log

Log different classes of messages to associated files. Log ids 0, 1, and 2 are reserved for stdin, stdout, and stderr whatever their respective numbers may be.


#define DBUG 0x00000010, __FILE__, __LINE__
#define WARN 0x00000020, __FILE__, __LINE__
#define ERRO 0x00000040, __FILE__, __LINE__
#define MEMY 0x00000100, __FILE__, __LINE__
#define NORM (0x10 | 0x20 | 0x40), __FILE__, __LINE__
#define ALL  (0x10 | 0x20 | 0x40 | 0x100), __FILE__, __LINE__

int log_init(unsigned int mask, const char *file, unsigned int line,
                                const char *filename,
                                const char *mode);
void log_deinit(void);
void log(unsigned int id, const char *file, unsigned int line, const char *format, ...);
void log_hexdump(unsigned int id, const char *file, unsigned int line,
                                const void *src,
                                unsigned int off,
                                unsigned int len,
                                unsigned int width,
                                const char *format,
                                ...);
void log_profile_report(unsigned int id, const char *file, unsigned int line);

2.7 encdec

Encode and decode primative objects such as integers, strings, and times.

2.7.1 integers

Encode integers.


size_t enc_uint16be(uint16_t s, char *dst);
size_t enc_uint32be(uint32_t i, char *dst);
size_t enc_uint16le(uint16_t s, char *dst);
size_t enc_uint32le(uint32_t i, char *dst);

Decode integers.


uint16_t dec_uint16be(const char *src);
uint32_t dec_uint32be(const char *src);
uint16_t dec_uint16le(const char *src);
uint32_t dec_uint32le(const char *src);

2.7.2 strings

The following encodings are supported.


enum encdec_string_encodings {
    STR_ASCII = 1,
    STR_CP1252,
    STR_ISO_8859_1,
    STR_UTF_8,
    STR_UTF_16BE,
    STR_UTF_16LE,
    STR_UCS_2BE,
    STR_UCS_2LE,
    STR_UCS_4BE,
    STR_UCS_4LE,
    STR_NUM_ENC
};

Encode n wide characters of src into dst as enc returning the number of bytes encoded which must not exceed max.


size_t enc_strn(const wchar_t *src, unsigned int n, char *dst, size_t max, int enc);

Decode n bytes of enc encoded src returning the number of resulting wide characters placed into dst which must not exceed max.


unsigned int dec_strn(const char *src,
                                size_t n,
                                wchar_t *dst,
                                unsigned int max,
                                int enc);
size_t dec_str_size(const char *src, size_t max, int enc);
wchar_t *dec_str_new(const char *src, size_t *size, unsigned int *len, int enc);

2.7.3 times

Encode and decode time_t. Currently only simple 32 bit representations are supported.


enum encdec_time_encodings {
    TIME_EPOCH_SEC_32BE = 1,
    TIME_EPOCH_SEC_32LE,
    TIME_NUM_ENC
};

size_t enc_time(const time_t *timep, char *dst, int enc);
time_t dec_time(const char *src, int enc);

2.8 profile

A simple memory profiler. These functions should not be used directly. Simply include the profile.h header within a source file to be profiled and call the profile_report function to display memory usage. Presumably this would be called after allocations were believed to have been freed.


void profile_del(void);
void *profile_calloc(size_t nmemb, size_t size, const char *file, unsigned int line);
void *profile_malloc(size_t size, const char *file, unsigned int line);
void profile_free(void *ptr);
void *profile_realloc(void *ptr, size_t size, const char *file, unsigned int line);
void profile_add(void *ptr, size_t size, const char *file, unsigned int line);

#define calloc(nmemb, size) profile_calloc((nmemb), (size), __FILE__, __LINE__)
#define malloc(size) profile_malloc((size), __FILE__, __LINE__)
#define free profile_free
#define realloc(ptr, size) profile_realloc((ptr), (size), __FILE__, __LINE__)
#define PROFILE_ADD(ptr, size) profile_add((ptr), (size), __FILE__, __LINE__)

void profile_report(FILE *out);