Supplier Docs

intro

Supplier is a memory management library.

build

You can build the code using the makefile. On a 64 bit machine you have to set CFLAGS="-fPIC"; to cross-compile to 32 bit you have to set CFLAGS="-m32" and LDFLAGS="-m elf_i386" instead. Moreover you have to set the LD_LIBRARY_PATH variable before running the test executable.

Although there is no support, it should run on Windows.

supplier

With a supplier you can replace a malloc with a supp_supply call. In this way you don't have to free the memory, destroying the supplier will release the memory.

Obviously you can use more than one supplier at a time.

void *supplier;
char *memory;
supplier = supp_create(0, 0);
if (!supplier)
    exit(-1);
memory = (char *) supp_supply(supplier, 64);
if (!memory)
    exit(-1);
strcpy(memory, "some text");
puts(memory);
supp_destroy(supplier);

An arraylist is a dynamic array similar to the Java ArrayList class, its elements are generic pointers. It's based on the supplier and you don't have to destroy it.

void *supplier;
void *arraylist;
int i;
supplier = supp_create(0, 0);
if (!supplier)
    exit(-1);
arraylist = arraylist_new(supplier, 0);
if (!arraylist)
    exit(-1);
if (!arraylist_add(arraylist, "an element"))
    exit(-1);
puts((char *) arraylist_remove(arraylist, 0));
for (i = 0; i<10; ++i)
    if (!arraylist_add(arraylist, "same element"))
        exit(-1);
for (i = 0; i<10; ++i)
    puts((char *) arraylist_get(arraylist, i));
supp_destroy(supplier);

A linkedlist is a list implementation similar to the Java LinkedList class. The APIs are very closer to the arraylist ones, see the header files for additional information.

A sortedmap is an associative array whose keys are strings. It is similar to the Java Map implementations or to the Python dictionaries; you can generate keys using the helper function.

Several functions for string manipulation are present too.

  • str_startswith is similar to the startsWith method of the Java String class, to the startswith method of the Python strings or to the starts-with function in XSL
  • str_endswith is similar to the endsWith method of the Java String class or to the endswith method of the Python strings
  • str_concat uses the supplier to concatenate strings, like the sum operator in Java and Python or the concat function in XSL
  • str_substring extract a limited substring, like the substring function in XSL
  • str_join concatenate strings, like with the Java StringBuilder class or with the join method of the Python strings
  • str_split split the string using whitespaces
  • str_clone clones the string

C strings are mutable so cloning them can be useful. There are no functions to extract an unlimited substring, you can simply access to string+begin, cloning if required. A function to generate keys for the sortedmap is also present, see the header file for more information.

usage

You can split strings as using the split method of the Python strings in the following way.

char *input = "-/-A strin-/-g to spli-/-t-/--/-";
char *sep = "-/-";
void *supplier;
void *tokens;
char *string;
char *next;
char **words;
int length;
int i;
supplier = supp_create(0, 0);
if (!supplier)
    exit(-1);
tokens = arraylist_new(supplier, 0);
if (!tokens)
    exit(-1);
string = input;
while (string) {
    next = strstr(string, sep);
    if (next) {
        string = str_substring(supplier, string, 0, next-string);
        if (!string)
            exit(-1);
        if (!arraylist_add(tokens, string))
            exit(-1);
        string = next+strlen(sep);
    }
    else {
        if (!arraylist_add(tokens, string))
            exit(-1);
        string = NULL;
    }
}
i = 0;
length = arraylist_size(tokens);
words = (char **) arraylist_elements(tokens);
puts("---Tokens---");
while (i<length) {
    puts(words[i]);
    ++i;
}
puts("------------");
supp_destroy(supplier);

Here there is an example of how to use these functions to replace text in a string with a high-level approach.

char *input = "I wrote this code for my purposes.";
void *supplier;
void *dictionary;
char **words;
int i;
void *tokens;
supplier = supp_create(0, 0);
if (!supplier)
    exit(-1);
dictionary = sortedmap_new(supplier);
if (!dictionary)
    exit(-1);
if (!sortedmap_put(dictionary, "I", "You"))
    exit(-1);
if (!sortedmap_put(dictionary, "my", "your"))
    exit(-1);
if (!sortedmap_put(dictionary, "me", "you"))
    exit(-1);
words = str_split(supplier, input);
if (!words)
    exit(-1);
i = 0;
while (words[i])
    ++i;
tokens = linkedlist_repack(supplier, i, (void **) words);
if (!tokens)
    exit(-1);
for (i = 0; i<linkedlist_size(tokens); ++i)
    if (sortedmap_containskey(dictionary, (char *) linkedlist_get(tokens, i)))
        if (!linkedlist_set(tokens, i, sortedmap_get(dictionary, (char *) linkedlist_get(tokens, i))))
            exit(-1);
for (i = 1; i<linkedlist_size(tokens); i+=2)
    if (!linkedlist_insert(tokens, i, " "))
        exit(-1);
puts(str_join(supplier, (char **) linkedlist_elements(tokens)));
supp_destroy(supplier);

notes

In order to get memory aligned to the double of the size of a void pointer, as usually malloc does, it's advised to create a supplier with step 2.