Split the mempool spinlock into two.

Added a ROUNDUP() macro.
Moved area_malloc/free into util.c.
Added missing license.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@7466 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
beveloper 2004-05-09 15:20:10 +00:00
parent 0a4a7dee30
commit 77eda14527
5 changed files with 101 additions and 39 deletions

View File

@ -1,5 +1,20 @@
/* Intel PRO/1000 Family Driver
* Copyright (C) 2004 Marcus Overhagen <marcus@overhagen.de>. All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies, and that both the
* copyright notice and this permission notice appear in supporting documentation.
*
* Marcus Overhagen makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
*
* MARCUS OVERHAGEN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL MARCUS
* OVERHAGEN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __DEBUG_H
#define __DEBUG_H

View File

@ -1,13 +1,30 @@
/* Intel PRO/1000 Family Driver
* Copyright (C) 2004 Marcus Overhagen <marcus@overhagen.de>. All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies, and that both the
* copyright notice and this permission notice appear in supporting documentation.
*
* Marcus Overhagen makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
*
* MARCUS OVERHAGEN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL MARCUS
* OVERHAGEN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <KernelExport.h>
#include <malloc.h>
#include "mempool.h"
#include "if_compat.h"
#include "util.h"
#include "debug.h"
spinlock pool_lock = 0;
spinlock mbuf_pool_lock = 0;
spinlock chunk_pool_lock = 0;
void *mbuf_pool = 0;
void *chunk_pool = 0;
@ -15,28 +32,11 @@ void *chunk_pool = 0;
void *mbuf_head = 0;
void *chunk_head = 0;
static void *
area_malloc(int size)
{
void *p;
size = (size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);
if (create_area("area_malloc", &p, B_ANY_KERNEL_ADDRESS, size, B_FULL_LOCK,0) < 0)
return 0;
return p;
}
static void
area_free(void *p)
{
delete_area(area_for(p));
}
int
mempool_init(int count)
{
int chunk_size = 2048;
int mbuf_size = (sizeof(struct mbuf) + 15) & ~15;
int mbuf_size = ROUNDUP(sizeof(struct mbuf), 16);
int chunk_alloc_size = chunk_size * (count + 1);
int mbuf_alloc_size = mbuf_size * (count + 1);
@ -51,18 +51,18 @@ mempool_init(int count)
chunk_pool = area_malloc(chunk_alloc_size);
if (!chunk_pool) {
TRACE("failed to allocate chunk storage of %d bytes\n", chunk_alloc_size);
ERROR("failed to allocate chunk storage of %d bytes\n", chunk_alloc_size);
return -1;
}
mbuf_pool = area_malloc(mbuf_alloc_size);
if (!mbuf_pool) {
TRACE("failed to allocate mbuf storage of %d bytes\n", mbuf_alloc_size);
ERROR("failed to allocate mbuf storage of %d bytes\n", mbuf_alloc_size);
area_free(chunk_pool);
return -1;
}
chunk_base = (char *)(((unsigned long)chunk_pool + 2047) & ~2047);
mbuf_base = (char *)(((unsigned long)mbuf_pool + 15) & ~15);
chunk_base = (char *)ROUNDUP((unsigned long)chunk_pool, 2048);
mbuf_base = (char *)ROUNDUP((unsigned long)mbuf_pool, 16);
for (i = 0; i < count; i++) {
chunk_pool_put(chunk_base + i * chunk_size);
@ -86,13 +86,13 @@ mbuf_pool_get(void)
void *p;
cpu_status s;
s = disable_interrupts();
acquire_spinlock(&pool_lock);
acquire_spinlock(&mbuf_pool_lock);
p = mbuf_head;
if (p)
mbuf_head = *(void **)p;
release_spinlock(&pool_lock);
release_spinlock(&mbuf_pool_lock);
restore_interrupts(s);
return p;
}
@ -102,12 +102,12 @@ mbuf_pool_put(void *p)
{
cpu_status s;
s = disable_interrupts();
acquire_spinlock(&pool_lock);
acquire_spinlock(&mbuf_pool_lock);
*(void **)p = mbuf_head;
mbuf_head = p;
release_spinlock(&pool_lock);
release_spinlock(&mbuf_pool_lock);
restore_interrupts(s);
}
@ -117,13 +117,13 @@ chunk_pool_get(void)
void *p;
cpu_status s;
s = disable_interrupts();
acquire_spinlock(&pool_lock);
acquire_spinlock(&chunk_pool_lock);
p = chunk_head;
if (p)
chunk_head = *(void **)p;
release_spinlock(&pool_lock);
release_spinlock(&chunk_pool_lock);
restore_interrupts(s);
return p;
}
@ -133,11 +133,11 @@ chunk_pool_put(void *p)
{
cpu_status s;
s = disable_interrupts();
acquire_spinlock(&pool_lock);
acquire_spinlock(&chunk_pool_lock);
*(void **)p = chunk_head;
chunk_head = p;
release_spinlock(&pool_lock);
release_spinlock(&chunk_pool_lock);
restore_interrupts(s);
}

View File

@ -1,5 +1,20 @@
/* Intel PRO/1000 Family Driver
* Copyright (C) 2004 Marcus Overhagen <marcus@overhagen.de>. All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies, and that both the
* copyright notice and this permission notice appear in supporting documentation.
*
* Marcus Overhagen makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
*
* MARCUS OVERHAGEN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL MARCUS
* OVERHAGEN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __MEMPOOL_H
#define __MEMPOOL_H

View File

@ -1,5 +1,20 @@
/* Intel PRO/1000 Family Driver
* Copyright (C) 2004 Marcus Overhagen <marcus@overhagen.de>. All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies, and that both the
* copyright notice and this permission notice appear in supporting documentation.
*
* Marcus Overhagen makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
*
* MARCUS OVERHAGEN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL MARCUS
* OVERHAGEN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <Errors.h>
#include <OS.h>
@ -10,12 +25,6 @@
#include "debug.h"
#include "util.h"
static inline uint32
round_to_pagesize(uint32 size)
{
return (size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);
}
area_id
map_mem(void **virt, void *phy, size_t size, uint32 protection, const char *name)
{
@ -28,7 +37,7 @@ map_mem(void **virt, void *phy, size_t size, uint32 protection, const char *name
offset = (uint32)phy & (B_PAGE_SIZE - 1);
phyadr = (char *)phy - offset;
size = round_to_pagesize(size + offset);
size = ROUNDUP(size + offset, B_PAGE_SIZE);
area = map_physical_memory(name, phyadr, size, B_ANY_KERNEL_BLOCK_ADDRESS, protection, &mapadr);
if (area < B_OK) {
ERROR("mapping '%s' failed, error 0x%lx (%s)\n", name, area, strerror(area));
@ -42,3 +51,20 @@ map_mem(void **virt, void *phy, size_t size, uint32 protection, const char *name
return area;
}
void *
area_malloc(size_t size)
{
void *p;
size = ROUNDUP(size, B_PAGE_SIZE);
if (create_area("area_malloc", &p, B_ANY_KERNEL_ADDRESS, size, B_FULL_LOCK, 0) < 0)
return 0;
return p;
}
void
area_free(void *p)
{
delete_area(area_for(p));
}

View File

@ -21,6 +21,12 @@
#include <OS.h>
area_id map_mem(void **virt, void *phy, size_t size, uint32 protection, const char *name);
area_id map_mem(void **virt, void *phy, size_t size, uint32 protection, const char *name);
void * area_malloc(size_t size);
void area_free(void *p);
// generic macro for rounding, can only be used for power of 2 blocksize
#define ROUNDUP(size, blocksize) (((size) + (blocksize) - 1) & ~((blocksize) - 1))
#endif