1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Copyright (c) 1997-1998 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Simple UNIX-style resource map routines
*/
#include <oskit/amm.h>
#include <errno.h>
#include "rmap.h"
static amm_entry_t *rmap_alloc_entry(amm_t *amm, oskit_addr_t addr,
oskit_size_t size, int flags);
static void rmap_free_entry(amm_t *amm, amm_entry_t *entry);
/*
* XXX need
* amm_submap(amm_t *amm, oskit_addr_t start, oskit_size_t size)
* for common sub-maps. Would mark the rest of the map unavailable.
*/
void rminit(struct rmap *map, long size, long addr, char *name, int nent)
{
oskit_addr_t saddr = (oskit_addr_t)addr;
oskit_addr_t eaddr = saddr + size;
strncpy(map->name, name, 32);
map->elts = 0;
map->maxelts = nent;
amm_init_gen(&map->amm, RMAP_FREE, 0,
rmap_alloc_entry, rmap_free_entry, 0, 0);
if (AMM_MINADDR < saddr)
(void)amm_modify(&map->amm, AMM_MINADDR, saddr - AMM_MINADDR,
RMAP_UNAVAIL, 0);
if (eaddr < AMM_MAXADDR)
(void)amm_modify(&map->amm, eaddr, AMM_MAXADDR - eaddr,
RMAP_UNAVAIL, 0);
}
/*
* XXX need:
* amm_alloc(amm_t *amm, oskit_addr_t addr, oskit_size_t size, int anywhere)
* style interface for the common case of allocating free resources.
*/
long rmalloc(struct rmap *map, long size)
{
amm_entry_t *entry;
oskit_addr_t addr;
int rc;
if (size <= 0)
panic("rmalloc: bad size");
addr = AMM_MINADDR;
entry = amm_find_gen(&map->amm, &addr, (oskit_size_t)size, RMAP_FREE,
-1, 0, 0, 0);
if (entry == 0)
return 0;
rc = amm_modify(&map->amm, addr, (oskit_size_t)size, RMAP_INUSE, 0);
if (rc == ENOMEM) {
rc = amm_modify(&map->amm, amm_entry_start(entry),
amm_entry_size(entry), RMAP_INUSE, 0);
if (rc)
panic("rmalloc: corrupt map");
printf("WARNING: %s: resources lost\n", map->name);
} else if (rc)
panic("rmalloc: corrupt map");
return (long)amm_entry_start(entry);
}
/*
* XXX need:
* amm_dealloc(amm_t *amm, oskit_addr_t addr, oskit_size_t size)
* style interface for the common case of deallocating resources.
*/
void rmfree(struct rmap *map, long size, long uaddr)
{
amm_entry_t *entry;
oskit_addr_t addr;
int rc;
if (size <= 0)
panic("rmfree: bad size");
addr = (oskit_addr_t)uaddr;
entry = amm_find_gen(&map->amm, &addr, (oskit_size_t)size, RMAP_INUSE,
-1, 0, 0, 0);
if (entry == 0)
panic("rmfree: corrupt map");
rc = amm_modify(&map->amm, addr, (oskit_size_t)size, RMAP_FREE, 0);
if (rc == ENOMEM)
printf("WARNING: %s: resources lost\n", map->name);
else if (rc)
panic("rmfree: corrupt map");
}
void rmdump(struct rmap *map)
{
printf("MAP `%s' (0x%x):\n", map->name, map);
amm_dump(&map->amm);
}
static amm_entry_t *
rmap_alloc_entry(amm_t *amm, oskit_addr_t addr, oskit_size_t size, int flags)
{
struct rmap *map = (struct rmap *)amm;
if (flags != RMAP_UNAVAIL) {
if (map->elts == map->maxelts)
return 0;
map->elts++;
}
return (amm_entry_t *)malloc(sizeof(amm_entry_t));
}
static void
rmap_free_entry(amm_t *amm, amm_entry_t *entry)
{
struct rmap *map = (struct rmap *)amm;
if (amm_entry_flags(entry) != RMAP_UNAVAIL)
map->elts--;
free((void *)entry);
}
|