Go to the first, previous, next, last section, table of contents.


2.3 Small Sample of Code Showing Usage of the Library

Below is a simple example of what you can do with the library. Please note that although it gives you some idea about the basic functionality of the library it is not really doing something useful.

main()
{
        diskheap_t *diskheap_p;
        unsigned int block_n, offset, size;
        char *str_p;
        int ret;

        /* create a new diskheap file called 'heap' */
        diskheap_p = diskheap_create("heap", 0, 0, 0, 0, 0L);

        /*
         * Store the string 'hello there' (size 12 bytes) in the heap.
         * The variables block_n and offset get set with the location
         * of the string.
         */
        ret = diskheap_store(diskheap_p, "hello there", 12, 0, &block_n,
                             &offset);
        /* ret should be checked against DISKHEAP_ERROR_NONE */

        /*
         * Update the 'hello there' string and replace with 'hello
         * there again'.  You pass in the block_n and offset variables
         * so the heap library can locate the 'hello there' string and
         * they are set with the location of the new string.
         */
        ret = diskheap_update(diskheap_p, "hello there again", 18, 0, block_n,
                              offset, 0, &block_n, &offset, 0L, 0L);
        /* ret should be checked against DISKHEAP_ERROR_NONE */

        /*
         * Lookup the block-number and offset in the heap.  This
         * returns an allocated buffer of memory containing the string
         * while the size variable is set with its length.
         */
        str_p = diskheap_retrieve(diskheap_p, block_n, offset, &size, 0L,
                &ret);
        /* str_p should be checked against NULL */

        printf("String '%s' (size %u) is at block #%u, offset %u\n",
                str_p, size, block_n, offset);
        free(str_p);
}


Go to the first, previous, next, last section, table of contents.

Diskheap Home Page. Copyright 2002 by Gray Watson