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