xuzhenbao commented on code in PR #437: URL: https://github.com/apache/celix/pull/437#discussion_r956127401
########## bundles/remote_services/remote_service_admin_shm_v2/shm_pool/src/shm_pool.c: ########## @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <shm_pool.h> +#include <shm_pool_private.h> +#include <celix_threads.h> +#include <tlsf.h> +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.h> +#include <sys/ipc.h> +#include <sys/shm.h> +#include <errno.h> +#include <assert.h> + + +struct shm_pool{ + celix_thread_mutex_t mutex;// projects below + int shmId; + void *shmStartAddr; + struct shm_pool_shared_info *sharedInfo; + celix_thread_t shmHeartbeatThread; + bool heartbeatThreadActive; + celix_thread_cond_t heartbeatThreadStoped; + tlsf_t allocator; +}; + +static void *shmPool_heartbeatThread(void *data); + +celix_status_t shmPool_create(size_t size, shm_pool_t **pool) { + celix_status_t status = CELIX_SUCCESS; + size_t normalizedSharedInfoSize = (sizeof(struct shm_pool_shared_info) % sizeof(void *) == 0) ? + sizeof(struct shm_pool_shared_info) : (sizeof(struct shm_pool_shared_info)+sizeof(void *))/sizeof(void *) * sizeof(void *); + if (size <= tlsf_size() + normalizedSharedInfoSize || pool == NULL) { + fprintf(stderr,"Shm pool: Shm size should be greater than %zu.\n", tlsf_size()); + status = CELIX_ILLEGAL_ARGUMENT; + goto shm_size_invalid; + } + + shm_pool_t *shmPool = (shm_pool_t *)malloc(sizeof(*shmPool)); + assert(shmPool != NULL); + + status = celixThreadMutex_create(&shmPool->mutex, NULL); + if(status != CELIX_SUCCESS) { + goto shm_pool_mutex_err; + } + + shmPool->shmId = shmget(IPC_PRIVATE, size, SHM_R | SHM_W); Review Comment: Specify the IPC_PRIVATE constant as the key value to the `shmget` when creating the IPC object, which always results in the creation of a new IPC object that is guaranteed to have a unique key. And we use domain datagram socket to transmit the `shmId`, other process can use 'shmat' to attach relevant shared memory. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org