在开辟好的共享内存内进行alloc/free的函数集(4)

来源:chinaunix 作者:hightman
  #define PTR_TO_HEAD(p)    (((mm_mem_head *)(p)) - 1)
#define HEAD_TO_PTR(p)    ((void *)(((mm_mem_head *)(p)) + 1))
#define MM mm_core
#define    MM_WORD    mm_mem_head

#if (defined (__GNUC__) && __GNUC__ >= 2)
#    define MM_PLATFORM_ALIGNMENT (__alignof__ (MM_WORD))
#else
#    define MM_PLATFORM_ALIGNMENT (sizeof(MM_WORD))
#endif
#define MM_ALIGN(n) (void*)((((size_t)(n)-1) & ~(MM_PLATFORM_ALIGNMENT-1)) + MM_PLATFORM_ALIGNMENT)

#include "mm.h"

/* MM-lock implement */
static int mm_init_lock(mm_mutex *lock) {
  union semun arg;
  int n = MM_SEM_NUM;

  if ((lock->semid = semget(IPC_PRIVATE, n, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) < 0) {
    return 0;
  }

  arg.val = 1;
  while (n--) {
    if (semctl(lock->semid, n, SETVAL, arg) < 0) {
     semctl(lock->semid, n, IPC_RMID, 0);
    }
  }
  return 1;

时间:2009-06-13 20:29 来源:chinaunix 作者:hightman 原文链接

好文,顶一下
(1)
50%
文章真差,踩一下
(1)
50%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量