038ae0343d6bae0fb61fa9d442b41ec605131c8d
1 /******************************************************************************
2 * FILE PURPOSE: Main Routines for HPLIB Shared Memory APIs
3 ******************************************************************************
4 * FILE NAME: hplib_shem.c
5 *
6 * The HPLIB provides a set of utility APIs which provide the following functionality
7 *
8 * REVISION HISTORY:
9 *
10 * Copyright (c) Texas Instruments Incorporated 2013
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
22 * distribution.
23 *
24 * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 */
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <fcntl.h>
47 #include <sys/mman.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #include <sys/ioctl.h>
51 #include "hplibmod.h"
52 #include "hplib.h"
53 #include "hplib_shm.h"
54 #include "hplib_loc.h"
55 #include <pthread.h>
56 #include <sched.h>
57 #include <linux/unistd.h>
58 #include <sys/syscall.h>
59 #include <sys/sysinfo.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <sys/shm.h>
64 static int shm_key = 100;
65 //static int shm_size = 8192;
66 static int shm_id = 0;
69 /**************************************************************************
70 * FUNCTION PURPOSE: Create Shared Memory Region
71 **************************************************************************/
72 void* hplib_shmCreate(int size)
73 {
74 hplib_shmInfo_T* pBase;
75 struct shmid_ds info;
76 shm_id = shmget(shm_key, size, IPC_CREAT | 0666 );
78 if (shm_id < 0)
79 {
80 printf("hplib_shmCreate: shmget error\n");
81 return NULL;
82 }
83 else
84 {
85 pBase = (hplib_shmInfo_T*)shmat(shm_id, 0,0);
86 if (pBase == (hplib_shmInfo_T*)-1)
87 printf("hplib_shmCreate: shmat error\n");
88 else
89 {
90 pBase->size = size;
91 pBase->free_offset = ((sizeof(hplib_shmInfo_T) +127)/128)*128;
92 }
93 shmctl(shm_id, IPC_STAT, &info);
95 return pBase;
96 }
97 }
99 /**************************************************************************
100 * FUNCTION PURPOSE: Open previously created Shared Memory Region
101 **************************************************************************/
102 void* hplib_shmOpen(void)
103 {
104 // void* pBase;
105 hplib_shmInfo_T* pBase;
106 //hplib_shmInfo_T* pShmBase;
107 struct shmid_ds info;
108 shm_id = shmget(shm_key, 0, 0666 );
109 if (shm_id < 0)
110 {
111 printf("hplib_shmOpen: shmget error\n");
112 return NULL;
113 }
114 else
115 {
116 shmctl(shm_id, IPC_STAT, &info);
118 /* shmat(shmid, addr, flag), addr 0 implies system chooses suitable
119 unused address at to which the attach the memory segment, flag =0
120 implies read/write access to the memory segment */
121 //pBase = shmat(shm_id, 0,0);
122 pBase = (hplib_shmInfo_T*)shmat(shm_id, 0,0);
123 if (pBase == (hplib_shmInfo_T*)-1)
124 {
125 printf("hplib_shmOpen: shmat error\n");
126 return NULL;
127 }
129 return pBase;
130 }
131 }
133 /**************************************************************************
134 * FUNCTION PURPOSE: Delete Shared Memory Segment
135 **************************************************************************/
136 hplib_RetValue hplib_shmDelete()
137 {
138 shm_id = shmget(shm_key, 0, 0666 );
139 if (shm_id < 0)
140 {
141 printf("hplib_shmOpen: shmget error\n");
142 return hplib_FAILURE;
143 }
144 else
145 {
146 int err=shmctl(shm_id, IPC_RMID, 0);
147 if(err<0)
148 {
149 printf("hplib_shmDelete: shmctl failed\n");
150 return hplib_FAILURE;
151 }
152 }
153 return hplib_OK;
154 }
156 /**************************************************************************
157 * FUNCTION PURPOSE: Add entry to Shared Memory Layout
158 **************************************************************************/
159 hplib_RetValue hplib_shmAddEntry(void* base, int size, hplib_shmEntryId_E entryId)
160 {
161 hplib_shmInfo_T* pBase = (hplib_shmInfo_T*) base;
163 if ((pBase->free_offset + size) > pBase->size)
164 {
165 printf("hplib_shmAddEntry: insufficient memory request, size %d\n", size);
166 return hplib_ERR_NOMEM;
167 }
169 if (pBase->toc[entryId] !=0)
170 {
171 return hplib_OK;
172 }
174 pBase->toc[entryId] = pBase->free_offset;
175 pBase->free_offset += ((size +127)/128)*128;
177 return hplib_OK;
178 }
180 void* hplib_shmGetEntry(void* base, hplib_shmEntryId_E entryId)
181 {
182 hplib_shmInfo_T* pBase = (hplib_shmInfo_T*) base;
183 return(base + pBase->toc[entryId]);
185 }