1 #define LOG_TAG "JAMSHMSERVICE"
9 #include <binder/MemoryHeapBase.h>
10 #include <binder/IServiceManager.h>
11 #include <binder/IPCThreadState.h>
12 #include <utils/Log.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
18 #include "BnAndroidShm.h"
19 #include "AndroidShm.h"
22 #include "JackConstants.h"
31 #include <linux/ashmem.h>
32 #include <cutils/ashmem.h>
34 #include "JackError.h"
36 #include <semaphore.h>
39 #define MEMORY_SIZE 10*1024
41 #define SEMAPHORE_NULL_CHAR '\0'
50 int AndroidShm::instantiate() {
51 defaultServiceManager()->addService(String16("com.samsung.android.jam.IAndroidShm"), new AndroidShm
); // SINGLETON WITH SAME NAME
55 int AndroidShm::sendCommand(const char* command
) {
56 ALOGI("I(pid:%d) send command is %s\n", getpid(), command
);
57 if(strcmp(command
, "semaphore") == 0) {
58 // print debug message about semaphore simulation
59 for(int i
= MAX_SEMAPHORE_MEMORY_COUNT
-1 ; i
>= 0; i
--) {
60 printf("index[%3d] = ptr[%p] name[%s]\n", i
, (mSemaphore
[i
] != NULL
)?mSemaphore
[i
]->getBase():0, mSemaphoreName
[i
]);
61 ALOGI("index[%3d] = ptr[%p] name[%s]\n", i
, (mSemaphore
[i
] != NULL
)?mSemaphore
[i
]->getBase():0, mSemaphoreName
[i
]);
67 int AndroidShm::testGetBufferByNewProcess() {
68 ALOGI("testGetBufferByNewProcess...");
70 int childPid
= fork();
73 ALOGI("I(pid%d) made a child process(pid:%d)", getpid(), childPid
);
74 ALOGI("I(pid%d) wait until child(%d) was finish", getpid(), childPid
);
76 // wait ÇÏÁö ¾ÊÀ¸¸é child process°¡ ³²¾Æ ÀÖÀ½.
77 ALOGI("child(%d) was finished. ", childPid
);
78 } else if(childPid
== 0) {
79 ALOGI("im a new child process(pid:%d) ", getpid());
80 if(-1 == execlp("/system/bin/getbufferclient","getbufferclient",NULL
)) {
81 ALOGE("failed to execute getbufferclient");
85 ALOGI("failed creating child process");
90 int AndroidShm::testGetBuffer() {
91 ALOGI("I(pid:%d) trying to test get buffer...", getpid());
92 sp
<IServiceManager
> sm
= defaultServiceManager();
93 ALOGI("get default ServiceManager is done");
95 //String16* serviceName = new String16("com.samsung.android.jam.IAndroidShm");
98 b
= sm
->getService(String16("com.samsung.android.jam.IAndroidShm"));
99 //ALOGI("getservice is done");
103 //ALOGI("AndroidShm is not working, waiting...");
108 sp
<IAndroidShm
> service
= interface_cast
<IAndroidShm
>(b
);
111 sp
<IMemoryHeap
> receiverMemBase
= service
->getBuffer(0);
113 unsigned int *base
= (unsigned int *) receiverMemBase
->getBase();
115 if(base
!= (unsigned int *) -1) {
116 ALOGD("AndroidShm::testGetBuffer base=%p Data=0x%x\n",base
, *base
);
118 ret
= (unsigned int)(*base
);
119 ALOGI("AndroidShm::testGetBuffer base=%p Data=0x%x CHANGED\n",base
, *base
);
122 ALOGE("Error shared memory not available\n");
127 sp
<IMemoryHeap
> AndroidShm::getBuffer(int index
) {
128 ALOGI("I(pid:%d) getBuffer index:%d", getpid(), index
);
129 if(index
< 0 || index
>= MAX_SHARED_MEMORY_COUNT
) {
130 ALOGE("error : out of index [%d]", index
);
133 return mMemHeap
[index
];
136 int AndroidShm::MemAlloc(unsigned int size
) {
137 ALOGI("try to allocate memory size[%d]", size
);
138 for(int i
= 0; i
< MAX_SHARED_MEMORY_COUNT
; i
++) {
139 if(mMemHeap
[i
] == NULL
) {
140 mMemHeap
[i
] = new MemoryHeapBase(size
);
141 if(mMemHeap
[i
] == NULL
){
142 ALOGI("fail to alloc, try one more...");
143 continue; // try one more.
148 ALOGE("fail to MemAlloc");
149 return -1; // fail to alloc
152 AndroidShm::AndroidShm() {
153 ALOGI("AndroidShm is created");
154 for(int i
= 0; i
< MAX_SHARED_MEMORY_COUNT
; i
++) {
157 mRegistryIndex
= 10000;
158 //mMemHeap = new MemoryHeapBase(MEMORY_SIZE);
159 //unsigned int *base = (unsigned int*) mMemHeap->getBase();
160 //*base = 0xdeadcafe;//
162 for(int j
= 0; j
< MAX_SEMAPHORE_MEMORY_COUNT
; j
++) {
163 mSemaphore
[j
] = NULL
;
164 memset(mSemaphoreName
[j
], SEMAPHORE_NULL_CHAR
, MAX_SEMAPHORE_NAME_LENGTH
);
168 AndroidShm::~AndroidShm() {
169 ALOGI("AndroidShm is destroyed");
170 for(int i
= 0; i
< MAX_SHARED_MEMORY_COUNT
; i
++) {
171 (mMemHeap
[i
]).clear();
173 for(int j
= 0; j
< MAX_SEMAPHORE_MEMORY_COUNT
; j
++) {
174 (mSemaphore
[j
]).clear();
178 //status_t AndroidShm::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
179 // return BnAndroidShm::onTransact(code, data, reply, flags);
182 int AndroidShm::allocShm(const int size
) { // if negative return value is error
183 ALOGI("try to alloc shared memory size[%d]", size
);
184 return MemAlloc(size
);
187 int AndroidShm::removeShm(const unsigned int index
) { // shared memory Á¦°Å
188 ALOGI("try to remove shared memory index[%d]", index
);
189 if(index
>= MAX_SHARED_MEMORY_COUNT
) {
190 ALOGE("remove shared memory: out of index");
193 (mMemHeap
[index
]).clear();
197 int AndroidShm::isAllocated(const unsigned int index
) { // allocated ¿©ºÎ È®ÀÎ
198 ALOGI("try to check the memory allocation index[%d]", index
);
199 if(index
>= MAX_SHARED_MEMORY_COUNT
) {
200 ALOGE("shared memory: out of index");
203 if(mMemHeap
[index
] == NULL
)
209 int AndroidShm::setRegistryIndex(const unsigned int index
) {
210 ALOGI("set registry index %d", index
);
211 mRegistryIndex
= index
;
215 int AndroidShm::getRegistryIndex() {
216 return mRegistryIndex
;
219 sp
<IMemoryHeap
> AndroidShm::InitSemaphore(const char* name
) {
220 ALOGI("init semaphore [%s]", name
);
221 for(int i
= 0; i
< MAX_SEMAPHORE_MEMORY_COUNT
; i
++) {
222 if(mSemaphoreName
[i
][0] == SEMAPHORE_NULL_CHAR
) {
223 mSemaphore
[i
] = new MemoryHeapBase(sizeof(sem_t
));
224 if(mSemaphore
[i
] == NULL
){
225 ALOGI("fail to alloc, try one more...");
228 if(sem_init((sem_t
*)(mSemaphore
[i
]->getBase()), 1, 0) == 0) {
229 strncpy(mSemaphoreName
[i
], name
, MAX_SEMAPHORE_NAME_LENGTH
- 1);
230 mSemaphoreName
[i
][MAX_SEMAPHORE_NAME_LENGTH
- 1] = '\0';
231 ALOGI("sem_init success");
232 return mSemaphore
[i
];
234 (mSemaphore
[i
]).clear();
235 ALOGE("sem_init failed null returned");
239 // find already exist name
240 if(strcmp(mSemaphoreName
[i
], name
) == 0) { // found
241 ALOGI("found - return alread allocated semaphore");
242 return mSemaphore
[i
];
246 ALOGE("sem_init failed null returned 2");