#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/fb.h>
 #include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h> 
#include <sys/ioctl.h> 
#include "common.h"

#define IPCKEY 0x437800

void *share_mem_init(char *file,int id,size_t length)
{
    int shm_id,i;
    key_t key; 
    void *p_map;
    key = ftok(file,id);
    printf("id:%d length:%d\n",id,length);
    if(key==-1)
    {
        dy_syslog(LOG_ERR,"ftok error:%s",strerror(errno));
        return NULL;
    } 
    // key = 0x437800 | id;
    dy_syslog(LOG_INFO,"key=%d\n",key) ;
    shm_id=shmget(key,length,IPC_CREAT|IPC_EXCL|0600); 
    if(shm_id==-1 && errno == EEXIST) {
        shm_id=shmget(key,length,0); 
    }
    if(shm_id==-1)
    {
        dy_syslog(LOG_ERR,"shmget error:%s",strerror(errno));
        return NULL;
    }
    dy_syslog(LOG_INFO,"shm_id=%d\n", shm_id) ;
    p_map=shmat(shm_id,NULL,0); 
    // memset(p_map,0xaa,length);
    return p_map;
}
