q.huang | 036b6cf | 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 |
|
q.huang | f99a5a1 | 2023-02-14 18:07:50 +0800 | [diff] [blame] | 21 | static int s_use_count=0;
|
| 22 |
|
q.huang | 036b6cf | 2023-01-10 14:29:20 +0800 | [diff] [blame] | 23 | typedef struct{
|
| 24 | int num_of_buffer;
|
| 25 | int size_of_buffer;
|
| 26 | int cur_index;
|
| 27 | }lynq_shm_type_config;
|
| 28 |
|
| 29 | typedef enum{
|
| 30 | shm_buf_type_256,
|
| 31 | shm_buf_type_1k,
|
| 32 | shm_buf_type_max,
|
| 33 | }lynq_shm_buf_type;
|
| 34 |
|
| 35 | lynq_shm_type_config s_lynq_shm_config[shm_buf_type_max]={
|
| 36 | {10,256,0},
|
| 37 | {5,1024,0}
|
| 38 | };
|
| 39 |
|
| 40 | static void * s_ril_shm_buf=(void*) -1L;
|
| 41 |
|
| 42 | static pthread_mutex_t s_shm_mtx = PTHREAD_MUTEX_INITIALIZER;
|
| 43 |
|
| 44 | bool get_cur_shem_buffer_index(int size, int* level, int* index)
|
| 45 | {
|
| 46 | pthread_mutex_lock(&s_shm_mtx);
|
| 47 | for(int i=0;i<shm_buf_type_max;i++)
|
| 48 | {
|
| 49 | if(size<=s_lynq_shm_config[i].size_of_buffer)
|
| 50 | {
|
| 51 | (*level)=i;
|
| 52 | (*index)=(s_lynq_shm_config[i].cur_index)%s_lynq_shm_config[i].num_of_buffer;
|
| 53 | s_lynq_shm_config[i].cur_index++;
|
| 54 | pthread_mutex_unlock(&s_shm_mtx);
|
| 55 | return true;
|
| 56 | }
|
| 57 | }
|
| 58 | pthread_mutex_unlock(&s_shm_mtx);
|
| 59 | return false;
|
| 60 | }
|
| 61 |
|
| 62 | bool get_shem_buffer_level(int size, int* level)
|
| 63 | {
|
| 64 | for(int i=0;i<shm_buf_type_max;i++)
|
| 65 | {
|
| 66 | if(size<=s_lynq_shm_config[i].size_of_buffer)
|
| 67 | {
|
| 68 | (*level)=i;
|
| 69 | return true;
|
| 70 | }
|
| 71 | }
|
| 72 | return false;
|
| 73 | }
|
| 74 |
|
| 75 |
|
| 76 | char* get_shem_buffer(int level,int index)
|
| 77 | {
|
| 78 | int offset=0;
|
| 79 | for(int i=0;i<level;i++)
|
| 80 | {
|
| 81 | offset+=s_lynq_shm_config[i].num_of_buffer*s_lynq_shm_config[i].size_of_buffer;
|
| 82 | }
|
| 83 | offset+=s_lynq_shm_config[level].size_of_buffer*index;
|
| 84 | return ((char*) s_ril_shm_buf)+offset;
|
| 85 | }
|
| 86 |
|
| 87 | int get_max_shem_buffer_size()
|
| 88 | {
|
| 89 | return s_lynq_shm_config[shm_buf_type_max-1].size_of_buffer;
|
| 90 | }
|
| 91 |
|
| 92 | int get_total_shem_buffer_size()
|
| 93 | {
|
| 94 | int total_size=0;
|
| 95 | for(int i=0;i<shm_buf_type_max;i++)
|
| 96 | {
|
| 97 | total_size+=s_lynq_shm_config[i].num_of_buffer*s_lynq_shm_config[i].size_of_buffer;
|
| 98 | }
|
| 99 | return total_size;
|
| 100 | }
|
| 101 |
|
| 102 | static int create_shm_common(int size,int flags)
|
| 103 | {
|
| 104 | RLOGD("create shared memory\n");
|
| 105 |
|
| 106 | // key_t key = ftok(shm_key, 's');
|
| 107 | key_t key=0x123456;
|
| 108 | if (key == -1)
|
| 109 | {
|
| 110 | RLOGE("ftok error.\n");
|
| 111 | return -1;
|
| 112 | }
|
| 113 | RLOGD("key is 0x%x\n", key);
|
| 114 |
|
| 115 | int shmid = shmget(key,size , flags);
|
| 116 | if (shmid == -1)
|
| 117 | {
|
| 118 | RLOGE("shmget error.\n");
|
| 119 | return -1;
|
| 120 | }
|
| 121 | RLOGD("shmid is %d\n", shmid);
|
| 122 |
|
| 123 | return shmid;
|
| 124 | }
|
| 125 |
|
| 126 | int create_shm()
|
| 127 | {
|
| 128 | shmid = create_shm_common(get_total_shem_buffer_size(),IPC_CREAT|0644);
|
| 129 |
|
| 130 | if(shmid==-1)
|
| 131 | {
|
| 132 | return -1;
|
| 133 | }
|
| 134 | s_ril_shm_buf = shmat(shmid, NULL, 0);
|
| 135 | if (s_ril_shm_buf == (void*) -1L)
|
| 136 | {
|
| 137 | RLOGE("shmat error.\n");
|
| 138 | return -1;
|
| 139 | }
|
| 140 | return 0;
|
| 141 | }
|
| 142 |
|
| 143 | void remove_shm()
|
| 144 | {
|
| 145 | if(shmid!=-1)
|
| 146 | {
|
q.huang | f99a5a1 | 2023-02-14 18:07:50 +0800 | [diff] [blame] | 147 | if (shmdt(s_ril_shm_buf) != 0)
|
q.huang | 036b6cf | 2023-01-10 14:29:20 +0800 | [diff] [blame] | 148 | {
|
q.huang | f99a5a1 | 2023-02-14 18:07:50 +0800 | [diff] [blame] | 149 | RLOGE("shmdt error.\n");
|
| 150 | }
|
q.huang | 036b6cf | 2023-01-10 14:29:20 +0800 | [diff] [blame] | 151 | shmid = -1;
|
| 152 | }
|
| 153 | s_ril_shm_buf = (void*) -1L;
|
| 154 | return ;
|
| 155 | }
|
| 156 |
|
| 157 | int ril_init_mem() |
| 158 | {
|
q.huang | f99a5a1 | 2023-02-14 18:07:50 +0800 | [diff] [blame] | 159 | pthread_mutex_lock(&s_shm_mtx);
|
| 160 | RLOGE("init begin, use count is %d.\n",s_use_count);
|
| 161 | if(s_use_count==0)
|
| 162 | {
|
| 163 | if(create_shm()!=0)
|
| 164 | { |
| 165 | RLOGE("init end, use count is %d.\n",s_use_count);
|
| 166 | pthread_mutex_unlock(&s_shm_mtx);
|
| 167 | return -1;
|
| 168 | }
|
q.huang | 036b6cf | 2023-01-10 14:29:20 +0800 | [diff] [blame] | 169 | }
|
q.huang | f99a5a1 | 2023-02-14 18:07:50 +0800 | [diff] [blame] | 170 | s_use_count++;
|
| 171 | RLOGE("init end, use count is %d.\n",s_use_count);
|
| 172 | pthread_mutex_unlock(&s_shm_mtx);
|
q.huang | 036b6cf | 2023-01-10 14:29:20 +0800 | [diff] [blame] | 173 | return 0;
|
| 174 | } |
| 175 | |
| 176 | void ril_deinit_mem()
|
| 177 | {
|
q.huang | f99a5a1 | 2023-02-14 18:07:50 +0800 | [diff] [blame] | 178 | pthread_mutex_lock(&s_shm_mtx);
|
| 179 |
|
| 180 | RLOGE("de-init begin, use count is %d.\n",s_use_count);
|
| 181 | if(s_use_count==1)
|
| 182 | {
|
| 183 | remove_shm();
|
| 184 | }
|
| 185 |
|
| 186 | if(s_use_count>0)
|
| 187 | {
|
| 188 | s_use_count--;
|
| 189 | }
|
| 190 | RLOGE("de-init end, use count is %d.\n",s_use_count);
|
| 191 | pthread_mutex_unlock(&s_shm_mtx);
|
q.huang | 036b6cf | 2023-01-10 14:29:20 +0800 | [diff] [blame] | 192 | return ;
|
| 193 | }
|
| 194 |
|