BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / bcm440x / mempool.c
blob946271b12445875d28f8caf5dafa5a2736da0681
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,
34 B_32_BIT_FULL_LOCK, 0) < 0) {
35 return 0;
37 return p;
40 void
41 area_free(void *p)
43 delete_area(area_for(p));
46 int
47 mempool_init(int count)
49 int chunk_size = 2048;
51 int chunk_alloc_size = chunk_size * (count + 1);
53 char *chunk_base;
55 int i;
57 chunk_pool = area_malloc(chunk_alloc_size);
58 if (!chunk_pool) {
59 return -1;
62 chunk_base = (char *)ROUNDUP((unsigned long)chunk_pool, 2048);
64 for (i = 0; i < count; i++) {
65 chunk_pool_put(chunk_base + i * chunk_size);
68 return 0;
71 void
72 mempool_exit(void)
74 area_free(chunk_pool);
77 void *
78 chunk_pool_get(void)
80 void *p;
81 cpu_status s;
82 s = disable_interrupts();
83 acquire_spinlock(&chunk_pool_lock);
85 p = chunk_head;
86 if (p)
87 chunk_head = *(void **)p;
89 release_spinlock(&chunk_pool_lock);
90 restore_interrupts(s);
91 return p;
94 void
95 chunk_pool_put(void *p)
97 cpu_status s;
98 s = disable_interrupts();
99 acquire_spinlock(&chunk_pool_lock);
101 *(void **)p = chunk_head;
102 chunk_head = p;
104 release_spinlock(&chunk_pool_lock);
105 restore_interrupts(s);