BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / bcm570x / mempool.c
blob02d571bb6b654b153f243a63a2f694c3f951bc36
1 /* Intel PRO/1000 Family Driver
2 * Copyright (C) 2004 Marcus Overhagen <marcus@overhagen.de>. All rights reserved.
4 * Permission to use, copy, modify and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted, provided
6 * that the above copyright notice appear in all copies, and that both the
7 * copyright notice and this permission notice appear in supporting documentation.
9 * Marcus Overhagen makes no representations about the suitability of this software
10 * for any purpose. It is provided "as is" without express or implied warranty.
12 * MARCUS OVERHAGEN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
13 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL MARCUS
14 * OVERHAGEN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
15 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <KernelExport.h>
20 #include <malloc.h>
21 #include "mempool.h"
23 spinlock chunk_pool_lock = 0;
24 void *chunk_pool = 0;
25 void *chunk_head = 0;
27 void *
28 area_malloc(size_t size)
30 void *p;
31 size = ROUNDUP(size, B_PAGE_SIZE);
33 if (create_area("area_malloc", &p, B_ANY_KERNEL_ADDRESS, size, B_FULL_LOCK, 0) < 0)
34 return 0;
35 return p;
38 void
39 area_free(void *p)
41 delete_area(area_for(p));
44 int
45 mempool_init(int count)
47 int chunk_size = 2048;
49 int chunk_alloc_size = chunk_size * (count + 1);
51 char *chunk_base;
53 int i;
55 chunk_pool = area_malloc(chunk_alloc_size);
56 if (!chunk_pool) {
57 return -1;
60 chunk_base = (char *)ROUNDUP((unsigned long)chunk_pool, 2048);
62 for (i = 0; i < count; i++) {
63 chunk_pool_put(chunk_base + i * chunk_size);
66 return 0;
69 void
70 mempool_exit(void)
72 area_free(chunk_pool);
75 void *
76 chunk_pool_get(void)
78 void *p;
79 cpu_status s;
80 s = disable_interrupts();
81 acquire_spinlock(&chunk_pool_lock);
83 p = chunk_head;
84 if (p)
85 chunk_head = *(void **)p;
87 release_spinlock(&chunk_pool_lock);
88 restore_interrupts(s);
89 return p;
92 void
93 chunk_pool_put(void *p)
95 cpu_status s;
96 s = disable_interrupts();
97 acquire_spinlock(&chunk_pool_lock);
99 *(void **)p = chunk_head;
100 chunk_head = p;
102 release_spinlock(&chunk_pool_lock);
103 restore_interrupts(s);