BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / ipro1000 / mempool.c
blobf7fd1a04be7c0b14709375b1829035642c2b91ec
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"
22 #include "if_compat.h"
23 #include "util.h"
24 #include "debug.h"
26 spinlock mbuf_pool_lock = 0;
27 spinlock chunk_pool_lock = 0;
29 void *mbuf_pool = 0;
30 void *chunk_pool = 0;
32 void *mbuf_head = 0;
33 void *chunk_head = 0;
35 int
36 mempool_init(int count)
38 int chunk_size = 2048;
39 int mbuf_size = ROUNDUP(sizeof(struct mbuf), 16);
41 int chunk_alloc_size = chunk_size * (count + 1);
42 int mbuf_alloc_size = mbuf_size * (count + 1);
44 char *chunk_base;
45 char *mbuf_base;
47 int i;
49 INIT_DEBUGOUT2("chunk_size %d, mbuf_size %d", chunk_size, mbuf_size);
50 INIT_DEBUGOUT2("chunk_alloc_size %d, mbuf_alloc_size %d", chunk_alloc_size, mbuf_alloc_size);
52 chunk_pool = area_malloc(chunk_alloc_size);
53 if (!chunk_pool) {
54 ERROROUT1("failed to allocate chunk storage of %d bytes\n", chunk_alloc_size);
55 return -1;
57 mbuf_pool = area_malloc(mbuf_alloc_size);
58 if (!mbuf_pool) {
59 ERROROUT1("failed to allocate mbuf storage of %d bytes\n", mbuf_alloc_size);
60 area_free(chunk_pool);
61 return -1;
64 chunk_base = (char *)ROUNDUP((unsigned long)chunk_pool, 2048);
65 mbuf_base = (char *)ROUNDUP((unsigned long)mbuf_pool, 16);
67 for (i = 0; i < count; i++) {
68 chunk_pool_put(chunk_base + i * chunk_size);
69 mbuf_pool_put(mbuf_base + i * mbuf_size);
72 INIT_DEBUGOUT("mempool init success");
73 return 0;
76 void
77 mempool_exit(void)
79 area_free(chunk_pool);
80 area_free(mbuf_pool);
83 void *
84 mbuf_pool_get(void)
86 void *p;
87 cpu_status s;
88 s = disable_interrupts();
89 acquire_spinlock(&mbuf_pool_lock);
91 p = mbuf_head;
92 if (p)
93 mbuf_head = *(void **)p;
95 release_spinlock(&mbuf_pool_lock);
96 restore_interrupts(s);
97 return p;
100 void
101 mbuf_pool_put(void *p)
103 cpu_status s;
104 s = disable_interrupts();
105 acquire_spinlock(&mbuf_pool_lock);
107 *(void **)p = mbuf_head;
108 mbuf_head = p;
110 release_spinlock(&mbuf_pool_lock);
111 restore_interrupts(s);
114 void *
115 chunk_pool_get(void)
117 void *p;
118 cpu_status s;
119 s = disable_interrupts();
120 acquire_spinlock(&chunk_pool_lock);
122 p = chunk_head;
123 if (p)
124 chunk_head = *(void **)p;
126 release_spinlock(&chunk_pool_lock);
127 restore_interrupts(s);
128 return p;
131 void
132 chunk_pool_put(void *p)
134 cpu_status s;
135 s = disable_interrupts();
136 acquire_spinlock(&chunk_pool_lock);
138 *(void **)p = chunk_head;
139 chunk_head = p;
141 release_spinlock(&chunk_pool_lock);
142 restore_interrupts(s);