source: trunk/xcache/xc_shm.c @ 1468

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

clean up shm handlers

  • Property svn:eol-style set to native
File size: 2.7 KB
RevLine 
[148]1#ifdef TEST
[1378]2#   ifdef HAVE_CONFIG_H
3#       include <config.h>
4#   endif
5#   include <limits.h>
6#   include <stdio.h>
[148]7#else
[1378]8#   include "xcache.h"
[148]9#endif
[1378]10#include "xc_shm.h"
[148]11
12#include <assert.h>
13#include <stdlib.h>
14#include <string.h>
[478]15
[163]16struct _xc_shm_scheme_t {
[1468]17    const char            *name;
18    const xc_shm_vtable_t *vtable;
[163]19};
[148]20static xc_shm_scheme_t xc_shm_schemes[10];
21
22void xc_shm_init_modules() /* {{{ */
23{
[1135]24    extern void xc_allocator_init();
[470]25#ifdef HAVE_XCACHE_TEST
[148]26    extern void xc_shm_malloc_register();
[470]27#endif
[148]28    extern void xc_shm_mmap_register();
[153]29
30    memset(xc_shm_schemes, 0, sizeof(xc_shm_schemes));
[1135]31    xc_allocator_init();
[470]32#ifdef HAVE_XCACHE_TEST
[153]33    xc_shm_malloc_register();
[470]34#endif
[148]35    xc_shm_mmap_register();
36}
37/* }}} */
[1468]38int xc_shm_scheme_register(const char *name, const xc_shm_vtable_t *vtable) /* {{{ */
[148]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;
[1468]44            xc_shm_schemes[i].vtable = vtable;
[148]45            return 1;
46        }
47    }
48    return 0;
49}
50/* }}} */
[1468]51const xc_shm_vtable_t *xc_shm_scheme_find(const char *name) /* {{{ */
[148]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) {
[1468]56            return xc_shm_schemes[i].vtable;
[148]57        }
58    }
59    return NULL;
60}
61/* }}} */
[163]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/* }}} */
[148]79xc_shm_t *xc_shm_init(const char *type, xc_shmsize_t size, int readonly_protection, const void *arg1, const void *arg2) /* {{{ */
80{
[1468]81    const xc_shm_vtable_t *vtable = xc_shm_scheme_find(type);
[148]82
[1468]83    if (vtable) {
84        xc_shm_t *shm = vtable->init(size, readonly_protection, arg1, arg2);
[148]85        if (shm) {
[1468]86            shm->vtable = vtable;
[148]87        }
88        return shm;
89    }
90
91    return NULL;
92}
93/* }}} */
94void xc_shm_destroy(xc_shm_t *shm) /* {{{ */
95{
[1468]96    shm->vtable->destroy(shm);
[148]97}
98/* }}} */
[1468]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.