Added more batch functionality, including the CALL GOTO and SHIFT
[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 #include <sys/types.h>
13 #include <sys/sem.h>
14 #include <assert.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include "debugtools.h"
18 #include "global.h"
19 #include "selectors.h"
20 #include "shm_fragment.h"
21 #include "shm_block.h"
22 #include "shm_semaph.h"
23 #include "dde_proc.h"
24 #include "xmalloc.h"
25
26 DEFAULT_DEBUG_CHANNEL(shm)
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   TRACE("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   TRACE("block was set up at 0x%08x, size=0x%04xKB, 1st usable=%02x\n",
57               (int )block,size/1024,first);
58 }
59
60 /* shm_attach_block: attach existing shm block, setup selectors
61  * shm_id - id of the block to attach.
62  * proc_idx - if not -1, puts this data into local mapping
63  * map - localy mapped info about this block.
64  */
65 /* NOTE: there is no check if this block is already attached.
66  *       Attaching the same block more than once - is possible
67  *       In case of doubt use shm_locate_block.
68  */
69 struct shm_block *shm_attach_block(int shm_id, int proc_idx,
70                                    struct local_shm_map *map)
71 {
72   struct shm_block *block;
73   struct shmid_ds ds;
74   struct local_shm_map *this;
75   
76   shmctl(shm_id, IPC_STAT, &ds );
77
78   block=(struct shm_block*)shmat(shm_id, NULL, 0);
79   if (block==NULL || block == (struct shm_block*) -1) return NULL;
80
81   this=(struct local_shm_map *)xmalloc(sizeof(*this));
82   this->next= shm_map;
83   shm_map   = this;
84   this->shm_id= shm_id;
85   this->ptr = block;
86   
87   if (proc_idx < 0)
88       this->proc_idx=block->proc_idx;
89   else 
90       this->proc_idx=proc_idx;
91
92   if (map != NULL) {
93      memcpy(map, this, sizeof(map));
94      map->next= NULL;           /* don't pass private info */
95   }
96
97   return block;
98 }
99
100 struct shm_block *shm_create_block(int first, int size, int *shm_id) 
101 {
102   struct shm_block *block;
103   
104   if (size==0)
105      size=SHM_MINBLOCK;
106   else
107      /* round up size to a multiple of SHM_MINBLOCK */
108      size= (size+SHM_MINBLOCK-1) & ~(SHM_MINBLOCK-1);
109   *shm_id= shmget ( IPC_PRIVATE, size ,0700);
110   if (*shm_id==-1)
111      return NULL;
112   block=shm_attach_block(*shm_id, curr_proc_idx, NULL);
113   if (block!=NULL)
114      shm_setup_block(block, first, size);
115
116   return block;
117 }
118
119 /*
120 ** Locate attached block. (return it, or NULL on failure)
121 ** shm_id is the block we look for.
122 ** *map - will get all the info related to this local map + proc_idx
123 **        (may be NULL)
124 ** *seg - will get the segment this block is attached to.
125 */
126 struct shm_block *shm_locate_attached_block(int shm_id,
127                                             struct local_shm_map *map)
128 {
129   struct local_shm_map *curr;
130   
131   for (curr=  shm_map ; curr != NULL ; curr= curr->next) {
132      if (curr->shm_id == shm_id) {
133         if (map) {
134             memcpy(map, curr, sizeof(*curr) );
135             map->next = NULL;   /* this is private info ! */
136         }
137         return curr->ptr;
138     }
139   }
140   
141   /* block not found !  */
142   return 0;
143 }
144
145 /* shm_locate_block: see shm_attach_block.
146    In addition to shm_attach_block, make sure this
147    block is not already attached.
148    */
149 struct shm_block *shm_locate_block(int shm_id, struct local_shm_map *map)
150 {
151   
152   struct shm_block *ret;
153   ret= shm_locate_attached_block(shm_id, map);
154   if (ret!=NULL)
155      return ret;
156   /* block not found ! , try to attach */
157   return shm_attach_block(shm_id, -1, map);
158 }
159
160 static void forget_attached(int shmid)
161 {
162   struct local_shm_map *curr, **point_to_curr;
163   
164   for (curr= shm_map,    point_to_curr= &shm_map ;
165        curr != NULL ;
166        curr= curr->next, point_to_curr= &curr->next ) {
167      if (curr->shm_id == shmid) {
168         *point_to_curr= curr->next;
169         return;
170      }
171   }
172 }
173
174 /* delete chain of shm blocks (pointing to each other)
175  * Do it in reverse order. (This is what the recursion is for)
176  */
177 void shm_delete_chain(int *shmid)
178 {
179   struct shm_block *block;
180
181   if (*shmid == -1)
182       return;
183
184   block= shm_locate_block(*shmid, NULL);
185   forget_attached( *shmid );
186   if (block == NULL)
187       return;
188   shm_delete_chain(&block->next_shm_id);
189   shmctl(*shmid, IPC_RMID, NULL);
190   *shmid=-1;
191   shmdt((char *)block);
192 }
193
194 #endif  /* CONFIG_IPC */