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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* ABAPI Broker */
/* Pete Rowley */
#include "stdio.h"
#include "slap.h"
#include "prlock.h"
#include "prerror.h"
#include "prcvar.h"
#include "prio.h"
static Slapi_Mutex *buffer_lock = 0;
/* circular api buffer */
typedef struct _THEABAPI
{
char *guid;
void **api;
struct _THEABAPI *next;
struct _THEABAPI *prev;
} ABAPI;
typedef struct _API_FEATURES
{
int refcount;
slapi_apib_callback_on_zero callback_on_zero;
Slapi_Mutex *lock;
} APIB_FEATURES;
static ABAPI *head = NULL;
static ABAPI **ABAPIBroker_FindInterface(char *guid);
static void ***ABAPIBroker_FindInterface_All(char *guid);
int
slapi_apib_register(char *guid, void **api)
{
int ret = -1;
ABAPI *item;
if (buffer_lock == 0) {
if (0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
/* badness */
return -1;
}
/* simple - we don't check for duplicates */
item = (ABAPI *)slapi_ch_malloc(sizeof(ABAPI));
if (item) {
item->guid = guid;
item->api = api;
slapi_lock_mutex(buffer_lock);
if (head == NULL) {
head = item;
head->next = head;
head->prev = head;
} else {
item->next = head;
item->prev = head->prev;
head->prev = item;
item->prev->next = item;
}
slapi_unlock_mutex(buffer_lock);
ret = 0;
}
return ret;
}
int
slapi_apib_unregister(char *guid)
{
int ret = -1;
ABAPI **api;
if (buffer_lock == 0)
return ret;
if (buffer_lock == 0) {
if (0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
/* badness */
return -1;
}
slapi_lock_mutex(buffer_lock);
if ((api = ABAPIBroker_FindInterface(guid)) != NULL) {
(*api)->prev->next = (*api)->next;
(*api)->next->prev = (*api)->prev;
if (*api == head) {
head = (*api)->next;
}
if (*api == head) /* must be the last item, turn off the lights */
head = 0;
(*api)->guid = NULL;
(*api)->api = NULL;
slapi_ch_free((void **)api);
*api = 0;
ret = 0;
}
slapi_unlock_mutex(buffer_lock);
return ret;
}
int
slapi_apib_get_interface(char *guid, void ***api)
{
int ret = -1;
ABAPI **theapi;
if (buffer_lock == 0)
return ret;
if (buffer_lock == 0) {
if (0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
/* badness */
return -1;
}
slapi_lock_mutex(buffer_lock);
if ((theapi = ABAPIBroker_FindInterface(guid)) != NULL) {
*api = (*theapi)->api;
if ((*api)[0]) {
slapi_apib_addref(*api);
}
ret = 0;
}
slapi_unlock_mutex(buffer_lock);
return ret;
}
int
slapi_apib_get_interface_all(char *guid, void ****api)
{
void ***retapi = NULL;
int idx = 0;
if (buffer_lock == 0)
return -1;
if (buffer_lock == 0) {
if (0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
/* badness */
return -1;
}
slapi_lock_mutex(buffer_lock);
retapi = ABAPIBroker_FindInterface_All(guid);
for (idx = 0; retapi && retapi[idx]; ++idx) {
void **theapi = retapi[idx];
if (theapi[0]) {
slapi_apib_addref(theapi);
}
}
*api = retapi;
slapi_unlock_mutex(buffer_lock);
return 0;
}
int
slapi_apib_make_reference_counted(void **api, slapi_apib_callback_on_zero callback_on_zero)
{
int ret = -1;
if (api[0] == 0) {
api[0] = slapi_ch_malloc(sizeof(APIB_FEATURES));
if (api[0]) {
((APIB_FEATURES *)(api[0]))->lock = slapi_new_mutex();
if (((APIB_FEATURES *)(api[0]))->lock) {
((APIB_FEATURES *)(api[0]))->refcount = 0; /* the ref count */
((APIB_FEATURES *)(api[0]))->callback_on_zero = callback_on_zero;
ret = 0;
} else
slapi_ch_free(&(api[0]));
}
}
return ret;
}
int
slapi_apib_addref(void **api)
{
int ret;
slapi_lock_mutex(((APIB_FEATURES *)(api[0]))->lock);
ret = ++(((APIB_FEATURES *)(api[0]))->refcount);
slapi_unlock_mutex(((APIB_FEATURES *)(api[0]))->lock);
return ret;
}
int
slapi_apib_release(void **api)
{
APIB_FEATURES *features = (APIB_FEATURES *)api[0];
PRBool must_free = PR_FALSE;
int ret;
slapi_lock_mutex(features->lock);
ret = --features->refcount;
if (features->refcount == 0 && features->callback_on_zero &&
features->callback_on_zero(api)) /* this should deregister the interface */
{
must_free = PR_TRUE;
}
slapi_unlock_mutex(features->lock);
if (must_free) {
slapi_destroy_mutex(features->lock);
slapi_ch_free((void **)&features);
}
return ret;
}
static ABAPI **
ABAPIBroker_FindInterface(char *guid)
{
static ABAPI *api = 0; /* simple gut feeling optimization for constant calls on same api */
ABAPI *start_api = api;
if (!api) {
start_api = api = head;
}
if (api) {
do {
if (0 == strcmp(guid, api->guid)) {
return &api;
}
api = api->next;
} while (api != start_api);
}
return 0;
}
static void ***
ABAPIBroker_FindInterface_All(char *guid)
{
ABAPI *api = NULL;
ABAPI *start_api = head;
void ***apilist = NULL;
int idx = 0;
api = start_api;
if (!api) {
return NULL;
}
do {
if (0 == strcmp(guid, api->guid)) {
apilist = (void ***)slapi_ch_realloc((char *)apilist, (idx + 2) * sizeof(void **));
apilist[idx++] = api->api;
apilist[idx] = NULL;
}
api = api->next;
} while (api != start_api);
return apilist;
}
|