- pre4:
[davej-history.git] / ipc / util.h
blobff4bfb0fc99780fd156435c2b10dd14214679974
1 /*
2 * linux/ipc/util.h
3 * Copyright (C) 1999 Christoph Rohland
5 * ipc helper functions (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
6 */
8 #define USHRT_MAX 0xffff
9 #define SEQ_MULTIPLIER (IPCMNI)
11 void sem_init (void);
12 void msg_init (void);
13 void shm_init (void);
15 struct ipc_ids {
16 int size;
17 int in_use;
18 int max_id;
19 unsigned short seq;
20 unsigned short seq_max;
21 struct semaphore sem;
22 spinlock_t ary;
23 struct ipc_id* entries;
26 struct ipc_id {
27 struct kern_ipc_perm* p;
31 void __init ipc_init_ids(struct ipc_ids* ids, int size);
33 /* must be called with ids->sem acquired.*/
34 int ipc_findkey(struct ipc_ids* ids, key_t key);
35 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size);
37 /* must be called with both locks acquired. */
38 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id);
40 int ipcperms (struct kern_ipc_perm *ipcp, short flg);
42 /* for rare, potentially huge allocations.
43 * both function can sleep
45 void* ipc_alloc(int size);
46 void ipc_free(void* ptr, int size);
48 extern inline void ipc_lockall(struct ipc_ids* ids)
50 spin_lock(&ids->ary);
53 extern inline struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
55 struct kern_ipc_perm* out;
56 int lid = id % SEQ_MULTIPLIER;
57 if(lid > ids->size)
58 return NULL;
60 out = ids->entries[lid].p;
61 return out;
64 extern inline void ipc_unlockall(struct ipc_ids* ids)
66 spin_unlock(&ids->ary);
68 extern inline struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
70 struct kern_ipc_perm* out;
71 int lid = id % SEQ_MULTIPLIER;
72 if(lid > ids->size)
73 return NULL;
75 spin_lock(&ids->ary);
76 out = ids->entries[lid].p;
77 if(out==NULL)
78 spin_unlock(&ids->ary);
79 return out;
82 extern inline void ipc_unlock(struct ipc_ids* ids, int id)
84 spin_unlock(&ids->ary);
87 extern inline int ipc_buildid(struct ipc_ids* ids, int id, int seq)
89 return SEQ_MULTIPLIER*seq + id;
92 extern inline int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
94 if(uid/SEQ_MULTIPLIER != ipcp->seq)
95 return 1;
96 return 0;
99 void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
100 void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
102 #ifdef __ia64__
103 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
104 # define ipc_parse_version(cmd) IPC_64
105 #else
106 int ipc_parse_version (int *cmd);
107 #endif