]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dope/server/linux/sharedmem.c
Inital import
[l4.git] / l4 / pkg / dope / server / linux / sharedmem.c
1 /*
2  * \brief   DOpE shared memory management module
3  * \date    2002-02-04
4  * \author  Norman Feske <no@atari.org>
5  *
6  * This component provides an abstraction for handling
7  * shared memory.
8  */
9
10 /*
11  * Copyright (C) 2002-2004  Norman Feske  <nf2@os.inf.tu-dresden.de>
12  * Technische Universitaet Dresden, Operating Systems Research Group
13  *
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.
17  */
18
19 #include <stdio.h>
20
21 #include "dopestd.h"
22 #include "module.h"
23 #include "thread.h"
24 #include "sharedmem.h"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <sys/mman.h>
30 #include <unistd.h>
31
32
33 static int ident_cnt = 0;
34
35 struct shared_memory {
36         int   fh;
37         s32   size;
38         char  fname[64];
39         void *addr;
40 };
41
42 int init_sharedmem(struct dope_services *d);
43
44
45 /*************************
46  *** SERVICE FUNCTIONS ***
47  *************************/
48
49
50 /*** ALLOCATE SHARED MEMORY BLOCK OF SPECIFIED SIZE ***/
51 static SHAREDMEM *shm_alloc(s32 size) {
52         SHAREDMEM *new = malloc(sizeof(SHAREDMEM));
53         if (!new) {
54                 ERROR(printf("SharedMemory(alloc): out of memory.\n"));
55                 return NULL;
56         }
57
58         /* open file */
59         sprintf(new->fname, "/tmp/dopevscr%d", ident_cnt++);
60         new->fh = open(new->fname, O_CREAT | O_RDWR | O_TRUNC, S_IRWXU);
61         new->size = size;
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);
66         return new;
67 }
68
69
70 /*** FREE SHARED MEMORY BLOCK ***/
71 static void shm_destroy(SHAREDMEM *sm) {
72         if (!sm) return;
73         munmap(sm->addr, sm->size);
74         close(sm->fh);
75         free(sm);
76 }
77
78
79 /*** RETURN THE ADRESS OF THE SHARED MEMORY BLOCK ***/
80 static void *shm_get_adr(SHAREDMEM *sm) {
81         if (!sm) return NULL;
82         return sm->addr;
83 }
84
85
86 /*** GENERATE A GLOBAL IDENTIFIER FOR THE SPECIFIED SHARED MEMORY BLOCK ***/
87 static void shm_get_ident(SHAREDMEM *sm, char *dst) {
88         if (!sm) return;
89         sprintf(dst, "size=0x%08X file=%s", (int)sm->size, sm->fname);
90 }
91
92
93 /*** SHARE MEMORY BLOCK TO ANOTHER THREAD ***/
94 static s32 shm_share(SHAREDMEM *sm, THREAD *dst_thread) {
95         return 0;
96 }
97
98
99
100 /****************************************
101  *** SERVICE STRUCTURE OF THIS MODULE ***
102  ****************************************/
103
104 static struct sharedmem_services sharedmem = {
105         shm_alloc,
106         shm_destroy,
107         shm_get_adr,
108         shm_get_ident,
109         shm_share,
110 };
111
112
113
114 /**************************
115  *** MODULE ENTRY POINT ***
116  **************************/
117
118 int init_sharedmem(struct dope_services *d) {
119
120         d->register_module("SharedMemory 1.0",&sharedmem);
121         return 1;
122 }