Release 950901
[wine] / ipc / shm_block.c
1 /***************************************************************************
2  * Copyright 1995, Technion, Israel Institute of Technology
3  * Electrical Eng, Software Lab.
4  * Author:    Michael Veksler.
5  ***************************************************************************
6  * File:      shm_block.c
7  * Purpose:   Treat a shared memory block.
8  ***************************************************************************
9  */
10 #ifdef CONFIG_IPC
11
12 #define inline __inline__
13 #include <sys/types.h>
14 #include <sys/sem.h>
15 #include <stdio.h>
16 #include <assert.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stddebug.h>
20 #include <debug.h>
21 #include <global.h>
22 #include "selectors.h"
23 #include "shm_fragment.h"
24 #include "shm_block.h"
25 #include "shm_semaph.h"
26 #include "dde_proc.h"
27
28 /* How each shmid is maped to local pointer */
29 /* Only attached shm blocks are in this construct */
30 struct local_shm_map *shm_map=NULL;
31
32 /* setup a new shm block (construct a shm block object).
33  *    block: The pointer to the memory block (local mapping)
34  *    first: The first data byte (excluding header stuff),
35  *           if 0  (zero) Use the default.
36  *    size:  The size of the memory block.
37  */
38 void shm_setup_block(struct shm_block *block, int first, int size)
39 {
40   dprintf_shm(stddeb,"Setting up shm block at 0x%08x\n",(int )block);
41   /* setup block internal data structure */
42   if (first <= 0) {
43      first=sizeof(*block);
44      /* round up - so everything starts on cache line boundary
45       * (assume cache line=32 bytes, may be bigger/smaller for
46       *  different processors and different L2 caches .)
47       */
48      first=(first+0x1f) & ~0x1f;
49   }
50   block->free=size-first;
51   block->next_shm_id=-1; /* IPC shm ID (for initial linking) */
52   block->proc_idx= curr_proc_idx;
53   /* block->size is initialized in shm_FragmentInit */
54   shm_FragmentInit(block, first, size);  /* first item in the free list */
55   
56   dprintf_shm(stddeb,
57               "block was set up at 0x%08x, size=0x%04xKB, 1st usable=%02x\n",
58               (int )block,size/1024,first);
59 }
60
61 /* shm_attach_block: attach existing shm block, setup selectors
62  * shm_id - id of the block to attach.
63  * proc_idx - if not -1, puts this data into local mapping
64  * map - localy mapped info about this block.
65  */
66 /* NOTE: there is no check if this block is already attached.
67  *       Attaching the same block more than once - is possible
68  *       In case of doubt use shm_locate_block.
69  */
70 struct shm_block *shm_attach_block(int shm_id, int proc_idx,
71                                    struct local_shm_map *map)
72 {
73   struct shm_block *block;
74   struct shmid_ds ds;
75   struct local_shm_map *this;
76   
77   shmctl(shm_id, IPC_STAT, &ds );
78
79   block=(struct shm_block*)shmat(shm_id, NULL, 0);
80   if (block==NULL || block == (struct shm_block*) -1) return NULL;
81
82   this=(struct local_shm_map *)malloc(sizeof(*this));
83   this->next= shm_map;
84   shm_map   = this;
85   this->shm_id= shm_id;
86   this->ptr = block;
87   
88   if (proc_idx < 0)
89       this->proc_idx=block->proc_idx;
90   else 
91       this->proc_idx=proc_idx;
92
93   if (map != NULL) {
94      memcpy(map, this, sizeof(map));
95      map->next= NULL;           /* don't pass private info */
96   }
97
98   return block;
99 }
100
101 struct shm_block *shm_create_block(int first, int size, int *shm_id) 
102 {
103   struct shm_block *block;
104   
105   if (size==0)
106      size=SHM_MINBLOCK;
107   else
108      /* round up size to a multiple of SHM_MINBLOCK */
109      size= (size+SHM_MINBLOCK-1) & ~(SHM_MINBLOCK-1);
110   *shm_id= shmget ( IPC_PRIVATE, size ,0700);
111   if (*shm_id==-1)
112      return NULL;
113   block=shm_attach_block(*shm_id, curr_proc_idx, NULL);
114   if (block!=NULL)
115      shm_setup_block(block, first, size);
116
117   return block;
118 }
119
120 /*
121 ** Locate attached block. (return it, or NULL on failure)
122 ** shm_id is the block we look for.
123 ** *map - will get all the info related to this local map + proc_idx
124 **        (may be NULL)
125 ** *seg - will get the segment this block is attached to.
126 */
127 struct shm_block *shm_locate_attached_block(int shm_id,
128                                             struct local_shm_map *map)
129 {
130   struct local_shm_map *curr;
131   
132   for (curr=  shm_map ; curr != NULL ; curr= curr->next) {
133      if (curr->shm_id == shm_id) {
134         if (map) {
135             memcpy(map, curr, sizeof(*curr) );
136             map->next = NULL;   /* this is private info ! */
137         }
138         return curr->ptr;
139     }
140   }
141   
142   /* block not found !  */
143   return 0;
144 }
145
146 /* shm_locate_block: see shm_attach_block.
147    In addition to shm_attach_block, make sure this
148    block is not already attached.
149    */
150 struct shm_block *shm_locate_block(int shm_id, struct local_shm_map *map)
151 {
152   
153   struct shm_block *ret;
154   ret= shm_locate_attached_block(shm_id, map);
155   if (ret!=NULL)
156      return ret;
157   /* block not found ! , try to attach */
158   return shm_attach_block(shm_id, -1, map);
159 }
160
161 static void forget_attached(int shmid)
162 {
163   struct local_shm_map *curr, **point_to_curr;
164   
165   for (curr= shm_map,    point_to_curr= &shm_map ;
166        curr != NULL ;
167        curr= curr->next, point_to_curr= &curr->next ) {
168      if (curr->shm_id == shmid) {
169         *point_to_curr= curr->next;
170         return;
171      }
172   }
173 }
174
175 /* delete chain of shm blocks (pointing to each other)
176  * Do it in reverse order. (This is what the recursion is for)
177  */
178 void shm_delete_chain(int *shmid)
179 {
180   struct shm_block *block;
181
182   if (*shmid == -1)
183       return;
184
185   block= shm_locate_block(*shmid, NULL);
186   forget_attached( *shmid );
187   if (block == NULL)
188       return;
189   shm_delete_chain(&block->next_shm_id);
190   shmctl(*shmid, IPC_RMID, NULL);
191   *shmid=-1;
192   shmdt((char *)block);
193 }
194
195 #endif  /* CONFIG_IPC */