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

Subversion Repositories c0or1k

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

powered by: WebSVN 2.1.0

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