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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [mm0/] [lib/] [idpool.c] - Blame information for rev 2

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 <lib/idpool.h>
7
#include <malloc/malloc.h>
8
#include <l4/macros.h>
9
#include INC_GLUE(memory.h)
10
#include <stdio.h>
11
#include <l4/api/errno.h>
12
 
13
struct id_pool *id_pool_new_init(int totalbits)
14
{
15
        int nwords = BITWISE_GETWORD(totalbits) + 1;
16
        struct id_pool *new = kzalloc((nwords * SZ_WORD)
17
                                      + sizeof(struct id_pool));
18
        if (!new)
19
                return PTR_ERR(-ENOMEM);
20
 
21
        new->nwords = nwords;
22
        new->bitlimit = totalbits;
23
 
24
        return new;
25
}
26
 
27
/* Search for a free slot up to the limit given */
28
int id_new(struct id_pool *pool)
29
{
30
        return find_and_set_first_free_bit(pool->bitmap, pool->bitlimit);
31
}
32
 
33
/* This finds n contiguous free ids, allocates and returns the first one */
34
int ids_new_contiguous(struct id_pool *pool, int numids)
35
{
36
        int id = find_and_set_first_free_contig_bits(pool->bitmap,
37
                                                     pool->bitlimit,
38
                                                     numids);
39
        if (id < 0)
40
                printf("%s: Warning! New id alloc failed\n", __FUNCTION__);
41
        return id;
42
}
43
 
44
/* This deletes a list of contiguous ids given the first one and number of ids */
45
int ids_del_contiguous(struct id_pool *pool, int first, int numids)
46
{
47
        int ret;
48
 
49
        if (pool->nwords * WORD_BITS < first + numids)
50
                return -1;
51
        if ((ret = check_and_clear_contig_bits(pool->bitmap, first, numids)))
52
                printf("%s: Error: Invalid argument range.\n", __FUNCTION__);
53
        return ret;
54
}
55
 
56
int id_del(struct id_pool *pool, int id)
57
{
58
        int ret;
59
 
60
        if (pool->nwords * WORD_BITS < id)
61
                return -1;
62
 
63
        if ((ret = check_and_clear_bit(pool->bitmap, id) < 0))
64
                printf("%s: Error: Could not delete id.\n", __FUNCTION__);
65
        return ret;
66
}
67
 
68
/* Return a specific id, if available */
69
int id_get(struct id_pool *pool, int id)
70
{
71
        int ret;
72
 
73
        ret = check_and_set_bit(pool->bitmap, id);
74
 
75
        if (ret < 0)
76
                return ret;
77
        else
78
                return id;
79
}
80
 
81
int id_is_empty(struct id_pool *pool)
82
{
83
        for (int i = 0; i < pool->nwords; i++)
84
                if (pool->bitmap[i])
85
                        return 0;
86
        return 1;
87
}
88
 

powered by: WebSVN 2.1.0

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