| 1 |
/* |
| 2 |
* Copyright (C) 2006 by Latchesar Ionkov <[email protected]> |
| 3 |
* Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#if HAVE_CONFIG_H |
| 26 |
#include "config.h" |
| 27 |
#endif |
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> |
| 31 |
#include <stdarg.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <pthread.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <stdint.h> |
| 36 |
#include <inttypes.h> |
| 37 |
|
| 38 |
#include "9p.h" |
| 39 |
#include "npfs.h" |
| 40 |
#include "npclient.h" |
| 41 |
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0, 1, 0, 2, 0, 1, 0, 0, |
| 77 |
}; |
| 78 |
|
| 79 |
Npcpool * |
| 80 |
npc_create_pool(u32 maxid) |
| 81 |
{ |
| 82 |
Npcpool *p; |
| 83 |
|
| 84 |
p = malloc(sizeof(*p)); |
| 85 |
if (!p) { |
| 86 |
np_uerror(ENOMEM); |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
p->maxid = maxid; |
| 91 |
pthread_mutex_init(&p->lock, NULL); |
| 92 |
pthread_cond_init(&p->cond, NULL); |
| 93 |
p->msize = 32; /* 256 ids */ |
| 94 |
p->map = malloc(p->msize); |
| 95 |
if (!p->map) { |
| 96 |
np_uerror(ENOMEM); |
| 97 |
free(p); |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
memset(p->map, 0, p->msize); |
| 102 |
return p; |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
npc_destroy_pool(Npcpool *p) |
| 107 |
{ |
| 108 |
if (p) { |
| 109 |
free(p->map); |
| 110 |
free(p); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
u32 |
| 115 |
npc_get_id(Npcpool *p) |
| 116 |
{ |
| 117 |
int i, n; |
| 118 |
u32 ret; |
| 119 |
u8 *pt; |
| 120 |
|
| 121 |
xpthread_mutex_lock(&p->lock); |
| 122 |
|
| 123 |
again: |
| 124 |
for(i = 0; i < p->msize; i++) |
| 125 |
if (p->map[i] != 0xFF) |
| 126 |
break; |
| 127 |
|
| 128 |
if (i>=p->msize && p->msize*8<p->maxid) { |
| 129 |
n = p->msize + 32; |
| 130 |
if (n*8 > p->maxid) |
| 131 |
n = p->maxid/8 + 1; |
| 132 |
|
| 133 |
pt = realloc(p->map, n); |
| 134 |
if (pt) { |
| 135 |
memset(pt + p->msize, 0, n - p->msize); |
| 136 |
p->map = pt; |
| 137 |
i = p->msize; |
| 138 |
p->msize = n; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (i >= p->msize) { |
| 143 |
pthread_cond_wait(&p->cond, &p->lock); |
| 144 |
goto again; |
| 145 |
} |
| 146 |
|
| 147 |
ret = m2id[p->map[i]]; |
| 148 |
p->map[i] |= 1 << ret; |
| 149 |
ret += i * 8; |
| 150 |
|
| 151 |
xpthread_mutex_unlock(&p->lock); |
| 152 |
return ret; |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
npc_put_id(Npcpool *p, u32 id) |
| 157 |
{ |
| 158 |
xpthread_mutex_lock(&p->lock); |
| 159 |
p->map[id / 8] &= ~(1 << (id % 8)); |
| 160 |
xpthread_mutex_unlock(&p->lock); |
| 161 |
pthread_cond_broadcast(&p->cond); |
| 162 |
} |