OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [mm0/] [tests/] [idpool_test/] [idpool.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Used for thread and space ids.
3
 *
4
 * Copyright (C) 2007 Bahadir Balban
5
 */
6
#include "idpool.h"
7
#include <stdio.h>
8
#include <stdlib.h>
9
 
10
struct id_pool *id_pool_new_init(int totalbits)
11
{
12
        int nwords = BITWISE_GETWORD(totalbits);
13
        struct id_pool *new = calloc(1, (nwords * SZ_WORD)
14
                                      + sizeof(struct id_pool));
15
        new->nwords = nwords;
16
        return new;
17
}
18
 
19
int id_new(struct id_pool *pool)
20
{
21
        int id = find_and_set_first_free_bit(pool->bitmap,
22
                                             pool->nwords * WORD_BITS);
23
        if (id < 0)
24
                printf("%s: Warning! New id alloc failed\n", __FUNCTION__);
25
        return id;
26
}
27
 
28
/* This finds n contiguous free ids, allocates and returns the first one */
29
int ids_new_contiguous(struct id_pool *pool, int numids)
30
{
31
        printf("%s: Enter\n", __FUNCTION__);
32
        int id = find_and_set_first_free_contig_bits(pool->bitmap,
33
                                                     pool->nwords *WORD_BITS,
34
                                                     numids);
35
        if (id < 0)
36
                printf("%s: Warning! New id alloc failed\n", __FUNCTION__);
37
        return id;
38
}
39
 
40
/* This deletes a list of contiguous ids given the first one and number of ids */
41
int ids_del_contiguous(struct id_pool *pool, int first, int numids)
42
{
43
        int ret;
44
        printf("bits: %d, first +nids: %d\n", pool->nwords *WORD_BITS, first+numids);
45
        if (pool->nwords * WORD_BITS < first + numids)
46
                return -1;
47
        if ((ret = check_and_clear_contig_bits(pool->bitmap, first, numids)))
48
                printf("Warning!!!\n");
49
        return ret;
50
}
51
 
52
int id_del(struct id_pool *pool, int id)
53
{
54
        int ret;
55
 
56
        if (pool->nwords * WORD_BITS < id)
57
                return -1;
58
 
59
        if ((ret = check_and_clear_bit(pool->bitmap, id) < 0))
60
                printf("Warning!!!\n");
61
        return ret;
62
}
63
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.