source: trunk/xcache/xc_shm.c @ 1468

Last change on this file since 1468 was 1468, checked in by moo, 5 months ago

clean up shm handlers

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1#ifdef TEST
2#   ifdef HAVE_CONFIG_H
3#       include <config.h>
4#   endif
5#   include <limits.h>
6#   include <stdio.h>
7#else
8#   include "xcache.h"
9#endif
10#include "xc_shm.h"
11
12#include <assert.h>
13#include <stdlib.h>
14#include <string.h>
15
16struct _xc_shm_scheme_t {
17    const char            *name;
18    const xc_shm_vtable_t *vtable;
19};
20static xc_shm_scheme_t xc_shm_schemes[10];
21
22void xc_shm_init_modules() /* {{{ */
23{
24    extern void xc_allocator_init();
25#ifdef HAVE_XCACHE_TEST
26    extern void xc_shm_malloc_register();
27#endif
28    extern void xc_shm_mmap_register();
29
30    memset(xc_shm_schemes, 0, sizeof(xc_shm_schemes));
31    xc_allocator_init();
32#ifdef HAVE_XCACHE_TEST
33    xc_shm_malloc_register();
34#endif
35    xc_shm_mmap_register();
36}
37/* }}} */
38int xc_shm_scheme_register(const char *name, const xc_shm_vtable_t *vtable) /* {{{ */
39{
40    int i;
41    for (i = 0; i < 10; i ++) {
42        if (!xc_shm_schemes[i].name) {
43            xc_shm_schemes[i].name = name;
44            xc_shm_schemes[i].vtable = vtable;
45            return 1;
46        }
47    }
48    return 0;
49}
50/* }}} */
51const xc_shm_vtable_t *xc_shm_scheme_find(const char *name) /* {{{ */
52{
53    int i;
54    for (i = 0; i < 10 && xc_shm_schemes[i].name; i ++) {
55        if (strcmp(xc_shm_schemes[i].name, name) == 0) {
56            return xc_shm_schemes[i].vtable;
57        }
58    }
59    return NULL;
60}
61/* }}} */
62xc_shm_scheme_t *xc_shm_scheme_first() /* {{{ */
63{
64    return xc_shm_schemes;
65}
66/* }}} */
67xc_shm_scheme_t *xc_shm_scheme_next(xc_shm_scheme_t *scheme) /* {{{ */
68{
69    scheme ++;
70    return scheme->name ? scheme : NULL;
71}
72/* }}} */
73const char *xc_shm_scheme_name(xc_shm_scheme_t *scheme) /* {{{ */
74{
75    assert(scheme);
76    return scheme->name;
77}
78/* }}} */
79xc_shm_t *xc_shm_init(const char *type, xc_shmsize_t size, int readonly_protection, const void *arg1, const void *arg2) /* {{{ */
80{
81    const xc_shm_vtable_t *vtable = xc_shm_scheme_find(type);
82
83    if (vtable) {
84        xc_shm_t *shm = vtable->init(size, readonly_protection, arg1, arg2);
85        if (shm) {
86            shm->vtable = vtable;
87        }
88        return shm;
89    }
90
91    return NULL;
92}
93/* }}} */
94void xc_shm_destroy(xc_shm_t *shm) /* {{{ */
95{
96    shm->vtable->destroy(shm);
97}
98/* }}} */
99
100int xc_shm_can_readonly(const xc_shm_t *shm) /* {{{ */
101{
102    return ((xc_shm_base_t *)shm)->readonlydiff != 0;
103}
104/* }}} */
105void *xc_shm_to_readwrite(const xc_shm_t *shm_, void *p) /* {{{ */
106{
107    const xc_shm_base_t *shm = (const xc_shm_base_t *) shm_;
108
109    if (shm->readonlydiff) {
110        assert(shm->vtable->is_readonly(shm_, p));
111        p = ((char *) p) - shm->readonlydiff;
112    }
113    assert(shm->vtable->is_readwrite(shm_, p));
114    return p;
115}
116/* }}} */
117void *xc_shm_to_readonly(const xc_shm_t *shm_, void *p) /* {{{ */
118{
119    const xc_shm_base_t *shm = (const xc_shm_base_t *) shm_;
120
121    assert(shm->vtable->is_readwrite(shm_, p));
122    if (shm->readonlydiff) {
123        p = ((char *) p) + shm->readonlydiff;
124        assert(shm->vtable->is_readonly(shm_, p));
125    }
126    return p;
127}
128/* }}} */
Note: See TracBrowser for help on using the repository browser.