0022627: Change OCCT memory management defaults
[occt.git] / src / OSD / OSD_Csharedmemory.c
CommitLineData
7fd59977 1#if !defined( WNT ) && !defined(__hpux) && !defined( HPUX)
2#include <stdio.h>
3#include <sys/types.h>
4#include <sys/ipc.h>
5#include <sys/shm.h>
6
7#if defined(HAVE_CONFIG_H)
8#include "config.h"
9#endif
10#if defined(HAVE_STDLIB_H)
11#include <stdlib.h>
12#endif
13#if defined(HAVE_MALLOC_H)
14#include <malloc.h>
15#endif
16
17/* Modified by Stephan GARNAUD (ARM) 1992 for Matra Datavision */
18
19
20static int status ;
21
22/* #ifdef ANSI */
23int osd_getkey(char *) ;
24int create_sharedmemory (int **,char *,int);
25int open_sharedmemory (int **,char *,int);
26int remove_sharedmemory (int * ,char * );
27/* #endif */
28
29
30int create_sharedmemory(int **section, char *section_name, int section_size)
31/*===============================
32
33 CREATE a mapping memory section
34
35 returns 0 if failed
36 otherwise successfull : the shared memory id
37
38===============================================================*/
39{
40
41 key_t key;
42 key = (key_t) osd_getkey (section_name);
43
44 *section = NULL ;
45 status = shmget( key,section_size,0750 + IPC_CREAT) ;
46
47 if( status < 0 ) return 0 ; /* shmget failed */
48 else {
49 *section = (int*)shmat(status,NULL,0) ; /* shmat failed */
50
51 if( *section == (int*)-1 ) {
52 *section = (int*)malloc(section_size) ;
53 return 0 ;
54 }
55 }
56 return status;
57}
58
59
60int open_sharedmemory(int **section, char *section_name, int section_size)
61/*=============================
62
63 OPEN a mapping memory section
64 We suppose that the shared memory segment is already
65 created(allocated)
66 Returns Value:
67 0 : If failed
68 otherwise successfull
69================================================================*/
70{
71
72 key_t key;
73 key = (key_t) osd_getkey (section_name);
74
75 *section = NULL ;
76 /* Test if shared memory identified by "k" exists ? */
77 status = shmget( key,0,0) ;
78
79 if( status < 0 ) return 0; /* shmget failed */
80
81 /* Try to attach the shared memory to the current process */
82 *section = (int*)shmat(status,NULL,0) ;
83
84 if( *section == (int*)-1 ) return 0; /* shmat failed */
85
86 return status ;
87}
88
89
90int remove_sharedmemory(int *shmid, char *section_name)
91/*===========================================
92
93 CLOSE a mapping memory section
94
95========================================================*/
96{
97 status = shmctl(*shmid ,IPC_RMID,NULL) ;
98 if (status < 0) return 0;
99 else return 1;
100}
101#endif