| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include "precompile.h"
- #include "q_malloc.h"
- #include "shm_mem.h"
- #include "shm.h"
- #include "strutil.h"
- #include "modCheck.h"
- # define MY_MALLOC qm_malloc
- # define MY_FREE qm_free
- # define shm_malloc_init qm_malloc_init
- //ʵÌå¼ä¹²Ïí
- TOOLKIT_API struct qm_block* shm_block = NULL;
- static int shm_shmid=-1; /*shared memory id*/
- static void* shm_mempool=(void*)-1;
- TOOLKIT_API void shm_lock()
- {
- qm_lock(shm_block);
- }
- TOOLKIT_API void shm_unlock()
- {
- qm_unlock(shm_block);
- }
- /* look at a buffer if there is perhaps enough space for the new size
- (It is beneficial to do so because vq_malloc is pretty stateful
- and if we ask for a new buffer size, we can still make it happy
- with current buffer); if so, we return current buffer again;
- otherwise, we free it, allocate a new one and return it; no
- guarantee for buffer content; if allocation fails, we return
- NULL
- */
- TOOLKIT_API int shm_getmem(int key, void **hint)
- {
- //struct shmid_ds shm_info;
- if ((shm_shmid!=-1)||(shm_mempool!=(void*)-1)){
- return 0; //Òѳõʼ»¯
- }
- shm_shmid=shmget(key, SHM_MEM_SIZE*1024*1024, IPC_CREAT);
- if (shm_shmid==-1){
- return -1;
- }
- shm_mempool=shmat(shm_shmid, hint ? *hint : NULL, 0);
- if (shm_mempool==(void*)-1){
- /* destroy segment*/
- shm_mem_destroy();
- return -1;
- }
- if (hint)
- *hint = shm_mempool;
- return 0;
- }
- static int shm_mem_init_mallocs(void* mempool, unsigned long pool_size, int newcreate)
- {
- if (newcreate) {
- /* init it for malloc*/
- shm_block=shm_malloc_init((char*)mempool, pool_size);
- if (shm_block==0){
- shm_mem_destroy();
- return -1;
- }
- } else {
- shm_block = (struct qm_block*)mempool;
- }
-
- return 0;
- }
- TOOLKIT_API int shm_mem_init2(int key, void *hint, int newcreate)
- {
- int ret;
- ret=shm_getmem(key, &hint);
- if (ret<0) return ret;
- return shm_mem_init_mallocs(shm_mempool, SHM_MEM_SIZE*1024*1024, newcreate);
- }
- TOOLKIT_API void shm_mem_destroy(void)
- {
- struct shmid_ds shm_info;
- if (shm_mempool && (shm_mempool!=(void*)-1)) {
- shmdt(shm_mempool);
- shm_mempool=(void*)-1;
- }
- if (shm_shmid!=-1) {
- shmctl(shm_shmid, IPC_RMID, &shm_info);
- shm_shmid=-1;
- }
- }
- TOOLKIT_API char *shm_strdup(const char *str)
- {
- int n;
- char *ret;
- if (!str)
- return NULL;
- n = strlen(str);
- ret = shm_malloc(n + 1);
- if (ret) {
- memcpy(ret, str, n+1);
- }
- return ret;
- }
- TOOLKIT_API char *shm_strdup_printf(const char *format, ...)
- {
- va_list arg;
- char *ret;
- va_start(arg, format);
- ret = shm_strdup_vprintf(format, arg);
- va_end(arg);
- return ret;
- }
- TOOLKIT_API char *shm_strdup_vprintf(const char *format, va_list args)
- {
- char *t = strdup_vprintf(format, args);
- char *r = shm_strdup(t);
- free(t);
- return r;
- }
- TOOLKIT_API void shm_set_user_data(int idx, void *user_data)
- {
- qm_set_user_data(shm_block, idx, user_data);
- }
- TOOLKIT_API void *shm_get_user_data(int idx)
- {
- if (shm_block == NULL)
- return NULL;
- return qm_get_user_data(shm_block, idx);
- }
- TOOLKIT_API void* shm_malloc(unsigned int size)
- {
- void *p;
- shm_lock();
- p=shm_malloc_unsafe(size);
- shm_unlock();
- return p;
- }
- TOOLKIT_API void *shm_malloc_unsafe(unsigned int _size)
- {
- return MY_MALLOC(shm_block, (_size));
- }
- TOOLKIT_API void shm_free_unsafe(void * _p )
- {
- MY_FREE(shm_block, (_p));
- }
|