q.huang | 63947df | 2023-01-10 14:29:20 +0800 | [diff] [blame^] | 1 | #include <stdio.h>
|
| 2 | #include <stdlib.h>
|
| 3 | #include <string.h>
|
| 4 | //#include <binder/Parcel.h>
|
| 5 | //#include <sys/socket.h>
|
| 6 | #include <errno.h>
|
| 7 | #include <stdbool.h>
|
| 8 | #include <sys/types.h>
|
| 9 | #include <unistd.h>
|
| 10 | #include "lynq_shm.h"
|
| 11 | #include <sys/shm.h>
|
| 12 | #include "log/log.h"
|
| 13 |
|
| 14 | #undef LOG_TAG
|
| 15 | #define LOG_TAG "SHM"
|
| 16 |
|
| 17 | #define shm_key "shm_key"
|
| 18 |
|
| 19 | int shmid=-1;
|
| 20 |
|
| 21 | typedef struct{
|
| 22 | int num_of_buffer;
|
| 23 | int size_of_buffer;
|
| 24 | int cur_index;
|
| 25 | }lynq_shm_type_config;
|
| 26 |
|
| 27 | typedef enum{
|
| 28 | shm_buf_type_256,
|
| 29 | shm_buf_type_1k,
|
| 30 | shm_buf_type_max,
|
| 31 | }lynq_shm_buf_type;
|
| 32 |
|
| 33 | lynq_shm_type_config s_lynq_shm_config[shm_buf_type_max]={
|
| 34 | {10,256,0},
|
| 35 | {5,1024,0}
|
| 36 | };
|
| 37 |
|
| 38 | static void * s_ril_shm_buf=(void*) -1L;
|
| 39 |
|
| 40 | static pthread_mutex_t s_shm_mtx = PTHREAD_MUTEX_INITIALIZER;
|
| 41 |
|
| 42 | bool get_cur_shem_buffer_index(int size, int* level, int* index)
|
| 43 | {
|
| 44 | pthread_mutex_lock(&s_shm_mtx);
|
| 45 | for(int i=0;i<shm_buf_type_max;i++)
|
| 46 | {
|
| 47 | if(size<=s_lynq_shm_config[i].size_of_buffer)
|
| 48 | {
|
| 49 | (*level)=i;
|
| 50 | (*index)=(s_lynq_shm_config[i].cur_index)%s_lynq_shm_config[i].num_of_buffer;
|
| 51 | s_lynq_shm_config[i].cur_index++;
|
| 52 | pthread_mutex_unlock(&s_shm_mtx);
|
| 53 | return true;
|
| 54 | }
|
| 55 | }
|
| 56 | pthread_mutex_unlock(&s_shm_mtx);
|
| 57 | return false;
|
| 58 | }
|
| 59 |
|
| 60 | bool get_shem_buffer_level(int size, int* level)
|
| 61 | {
|
| 62 | for(int i=0;i<shm_buf_type_max;i++)
|
| 63 | {
|
| 64 | if(size<=s_lynq_shm_config[i].size_of_buffer)
|
| 65 | {
|
| 66 | (*level)=i;
|
| 67 | return true;
|
| 68 | }
|
| 69 | }
|
| 70 | return false;
|
| 71 | }
|
| 72 |
|
| 73 |
|
| 74 | char* get_shem_buffer(int level,int index)
|
| 75 | {
|
| 76 | int offset=0;
|
| 77 | for(int i=0;i<level;i++)
|
| 78 | {
|
| 79 | offset+=s_lynq_shm_config[i].num_of_buffer*s_lynq_shm_config[i].size_of_buffer;
|
| 80 | }
|
| 81 | offset+=s_lynq_shm_config[level].size_of_buffer*index;
|
| 82 | return ((char*) s_ril_shm_buf)+offset;
|
| 83 | }
|
| 84 |
|
| 85 | int get_max_shem_buffer_size()
|
| 86 | {
|
| 87 | return s_lynq_shm_config[shm_buf_type_max-1].size_of_buffer;
|
| 88 | }
|
| 89 |
|
| 90 | int get_total_shem_buffer_size()
|
| 91 | {
|
| 92 | int total_size=0;
|
| 93 | for(int i=0;i<shm_buf_type_max;i++)
|
| 94 | {
|
| 95 | total_size+=s_lynq_shm_config[i].num_of_buffer*s_lynq_shm_config[i].size_of_buffer;
|
| 96 | }
|
| 97 | return total_size;
|
| 98 | }
|
| 99 |
|
| 100 | static int create_shm_common(int size,int flags)
|
| 101 | {
|
| 102 | RLOGD("create shared memory\n");
|
| 103 |
|
| 104 | // key_t key = ftok(shm_key, 's');
|
| 105 | key_t key=0x123456;
|
| 106 | if (key == -1)
|
| 107 | {
|
| 108 | RLOGE("ftok error.\n");
|
| 109 | return -1;
|
| 110 | }
|
| 111 | RLOGD("key is 0x%x\n", key);
|
| 112 |
|
| 113 | int shmid = shmget(key,size , flags);
|
| 114 | if (shmid == -1)
|
| 115 | {
|
| 116 | RLOGE("shmget error.\n");
|
| 117 | return -1;
|
| 118 | }
|
| 119 | RLOGD("shmid is %d\n", shmid);
|
| 120 |
|
| 121 | return shmid;
|
| 122 | }
|
| 123 |
|
| 124 | int create_shm()
|
| 125 | {
|
| 126 | shmid = create_shm_common(get_total_shem_buffer_size(),IPC_CREAT|0644);
|
| 127 |
|
| 128 | if(shmid==-1)
|
| 129 | {
|
| 130 | return -1;
|
| 131 | }
|
| 132 | s_ril_shm_buf = shmat(shmid, NULL, 0);
|
| 133 | if (s_ril_shm_buf == (void*) -1L)
|
| 134 | {
|
| 135 | RLOGE("shmat error.\n");
|
| 136 | return -1;
|
| 137 | }
|
| 138 | return 0;
|
| 139 | }
|
| 140 |
|
| 141 | void remove_shm()
|
| 142 | {
|
| 143 | if(shmid!=-1)
|
| 144 | {
|
| 145 | if (shmctl(shmid, IPC_RMID, NULL) == -1)
|
| 146 | {
|
| 147 | RLOGE("shmctl error.\n");
|
| 148 | }
|
| 149 | shmid = -1;
|
| 150 | }
|
| 151 | s_ril_shm_buf = (void*) -1L;
|
| 152 | return ;
|
| 153 | }
|
| 154 |
|
| 155 | int ril_init_mem() |
| 156 | {
|
| 157 | if(create_shm()!=0)
|
| 158 | { |
| 159 | return -1;
|
| 160 | }
|
| 161 | return 0;
|
| 162 | } |
| 163 | |
| 164 | void ril_deinit_mem()
|
| 165 | {
|
| 166 | remove_shm();
|
| 167 | return ;
|
| 168 | }
|
| 169 |
|