Locate the shared memory through 'key' or create a new one. */
| 107 | |
| 108 | /* Locate the shared memory through 'key' or create a new one. */ |
| 109 | static int _lwp_shmget(size_t key, size_t size, int create) |
| 110 | { |
| 111 | int id = -1; |
| 112 | struct lwp_avl_struct *node_key = 0; |
| 113 | struct lwp_avl_struct *node_pa = 0; |
| 114 | void *page_addr = 0; |
| 115 | uint32_t bit = 0; |
| 116 | |
| 117 | /* try to locate the item with the key in the binary tree */ |
| 118 | node_key = lwp_avl_find(key, shm_tree_key); |
| 119 | if (node_key) |
| 120 | { |
| 121 | return (struct lwp_shm_struct *)node_key->data - _shm_ary; /* the index */ |
| 122 | } |
| 123 | |
| 124 | /* If there doesn't exist such an item and we're allowed to create one ... */ |
| 125 | if (create) |
| 126 | { |
| 127 | struct lwp_shm_struct* p; |
| 128 | |
| 129 | if (!size) |
| 130 | { |
| 131 | goto err; |
| 132 | } |
| 133 | |
| 134 | id = _shm_id_alloc(); |
| 135 | if (id == -1) |
| 136 | { |
| 137 | goto err; |
| 138 | } |
| 139 | |
| 140 | /* allocate pages up to 2's exponent to cover the required size */ |
| 141 | bit = rt_page_bits(size); |
| 142 | page_addr = rt_pages_alloc_ext(bit, PAGE_ANY_AVAILABLE); /* virtual address */ |
| 143 | if (!page_addr) |
| 144 | { |
| 145 | goto err; |
| 146 | } |
| 147 | |
| 148 | /* initialize the shared memory structure */ |
| 149 | p = _shm_ary + id; |
| 150 | p->addr = (size_t)page_addr; |
| 151 | p->size = (1UL << (bit + ARCH_PAGE_SHIFT)); |
| 152 | p->ref = 0; |
| 153 | p->key = key; |
| 154 | p->mem_obj.get_name = get_shm_name; |
| 155 | p->mem_obj.on_page_fault = on_shm_page_fault; |
| 156 | p->mem_obj.on_varea_open = on_shm_varea_open; |
| 157 | p->mem_obj.on_varea_close = on_shm_varea_close; |
| 158 | p->mem_obj.hint_free = NULL; |
| 159 | |
| 160 | /* then insert it into the balancing binary tree */ |
| 161 | node_key = (struct lwp_avl_struct *)rt_malloc(sizeof(struct lwp_avl_struct) * 2); |
| 162 | if (!node_key) |
| 163 | { |
| 164 | goto err; |
| 165 | } |
| 166 | node_key->avl_key = p->key; |
no test coverage detected