2 * \brief DOpE shared memory management module
4 * \author Norman Feske <no@atari.org>
6 * This component provides an abstraction for handling
11 * Copyright (C) 2002-2004 Norman Feske <nf2@os.inf.tu-dresden.de>
12 * Technische Universitaet Dresden, Operating Systems Research Group
14 * This file is part of the DOpE package, which is distributed under
15 * the terms of the GNU General Public Licence 2. Please see the
16 * COPYING file for details.
24 #include "sharedmem.h"
26 #include <sys/types.h>
33 static int ident_cnt = 0;
35 struct shared_memory {
42 int init_sharedmem(struct dope_services *d);
45 /*************************
46 *** SERVICE FUNCTIONS ***
47 *************************/
50 /*** ALLOCATE SHARED MEMORY BLOCK OF SPECIFIED SIZE ***/
51 static SHAREDMEM *shm_alloc(s32 size) {
52 SHAREDMEM *new = malloc(sizeof(SHAREDMEM));
54 ERROR(printf("SharedMemory(alloc): out of memory.\n"));
59 sprintf(new->fname, "/tmp/dopevscr%d", ident_cnt++);
60 new->fh = open(new->fname, O_CREAT | O_RDWR | O_TRUNC, S_IRWXU);
62 ftruncate(new->fh, new->size);
63 new->addr = mmap(NULL, new->size, PROT_READ | PROT_WRITE,
64 MAP_SHARED, new->fh, 0);
65 printf("SharedMem(alloc): mmap file %s to addr %x\n", new->fname, (int)new->addr);
70 /*** FREE SHARED MEMORY BLOCK ***/
71 static void shm_destroy(SHAREDMEM *sm) {
73 munmap(sm->addr, sm->size);
79 /*** RETURN THE ADRESS OF THE SHARED MEMORY BLOCK ***/
80 static void *shm_get_adr(SHAREDMEM *sm) {
86 /*** GENERATE A GLOBAL IDENTIFIER FOR THE SPECIFIED SHARED MEMORY BLOCK ***/
87 static void shm_get_ident(SHAREDMEM *sm, char *dst) {
89 sprintf(dst, "size=0x%08X file=%s", (int)sm->size, sm->fname);
93 /*** SHARE MEMORY BLOCK TO ANOTHER THREAD ***/
94 static s32 shm_share(SHAREDMEM *sm, THREAD *dst_thread) {
100 /****************************************
101 *** SERVICE STRUCTURE OF THIS MODULE ***
102 ****************************************/
104 static struct sharedmem_services sharedmem = {
114 /**************************
115 *** MODULE ENTRY POINT ***
116 **************************/
118 int init_sharedmem(struct dope_services *d) {
120 d->register_module("SharedMemory 1.0",&sharedmem);