Example #1
0
void* hash_put(struct hash* table, char* key, void* data){

    unsigned long val = calc_hash(key) % table->size;
    hash_bucket* bucket;

    if ((table->table)[val] == NULL) {
        bucket = (hash_bucket*)malloc(sizeof(hash_bucket));
        if (bucket == NULL)
        	OUT_OF_MEM();

        bucket->key = strdup(key);
        bucket->next = NULL;
        bucket->data = data;

        (table->table)[val] = bucket;
        table->n++;
        table->used++;

        return bucket->data;
    }

    for (bucket = (table->table)[val]; bucket != NULL; bucket = bucket->next)
        if (strcmp(key, bucket->key) == 0) {
            void* old_data = bucket->data;

            bucket->data = data;

            return old_data;
        }

    bucket = (hash_bucket*) malloc(sizeof(hash_bucket));
    if (bucket == NULL)
    	OUT_OF_MEM();

    bucket->key = strdup(key);
    bucket->data = data;
    bucket->next = (table->table)[val];

    (table->table)[val] = bucket;
    table->n++;

    return data;
}
Example #2
0
File: mem.c Project: bsc-pm/mcxx
char *xstrdup(const char *s)
{
    char* result = strdup(s);

    if (result == NULL)
    {
        OUT_OF_MEM(strlen(s) + 1);
    }

    return result;
}
Example #3
0
rstruct newrstruct()
{
	rstruct hlp;
	hlp = (rstruct) calloc(1, sizeof(struct r_struct));
	if(hlp == NULL) {
#ifdef DEBUG
		perror("newrstruct 1");
#endif
		OUT_OF_MEM();
	}
	return hlp;
}				/* newrstruct */
Example #4
0
d_class newdclass()
{
	d_class hlp;
	hlp = (d_class) calloc(1, sizeof(struct dclass));
	if(hlp == NULL) {
#ifdef DEBUG
		perror("newdclass 1");
#endif
		OUT_OF_MEM();
	}
	return hlp;
}				/* newdclass */
Example #5
0
monoid newmon()
{
	monoid hlp;
	hlp = (monoid) calloc(1, sizeof(struct mono));
	if(hlp == NULL) {
#ifdef DEBUG
		perror("newmon 1");
#endif
		OUT_OF_MEM();
	}
	return hlp;
}				/* newmon */
Example #6
0
darray newdarray(posint a)
{
	darray hlp;
	hlp = (darray) calloc(a, sizeof(d_class));
	if(hlp == NULL) {
#ifdef DEBUG
		perror("newdarray 1");
#endif
		OUT_OF_MEM();
	}
	return hlp;
}				/* newdarray */
Example #7
0
struct hash* hash_create(long size){

	struct hash* table;
    hash_bucket** bucket;
    long i;

    table = (struct hash*)malloc(sizeof(struct hash));
    if(table == NULL){
    	OUT_OF_MEM();
    }

    if (size <= 0){
        free(table);
        return NULL;
    }

    table->size = size;
    table->table = (hash_bucket**)malloc(sizeof(hash_bucket*) * size);
    if(table->table == NULL)
    	OUT_OF_MEM();

    bucket = table->table;

    if (bucket == NULL) {
        free(table);
        return NULL;
    }

    for (i = 0; i < size; i++)
        bucket[i] = NULL;

    table->n = 0;
    table->used = 0;

    return table;
}
Example #8
0
File: mem.c Project: bsc-pm/mcxx
void *xmalloc(size_t size)
{
    if (size == 0)
        return NULL;

    void* ptr = malloc(size);
    if (ptr == NULL)
    {
        OUT_OF_MEM(size);
    }
    else
    {
        return ptr;
    }
}
Example #9
0
File: mem.c Project: bsc-pm/mcxx
void *xcalloc(size_t nmemb, size_t size)
{
    if (nmemb == 0
            || size == 0)
        return NULL;

    void* ptr = calloc(nmemb, size);
    if (ptr == NULL)
    {
        OUT_OF_MEM(nmemb * size);
    }
    else
    {
        return ptr;
    }
}
Example #10
0
File: mem.c Project: bsc-pm/mcxx
void *xrealloc(void *ptr, size_t size)
{
    if (size == 0)
    {
        xfree(ptr);
        return NULL;
    }
    else
    {
        void *res = realloc(ptr, size);
        if (res == NULL)
        {
            OUT_OF_MEM(size);
        }
        else
        {
            return res;
        }
    }
}