From celix-dev-return-195-apmail-incubator-celix-dev-archive=incubator.apache.org@incubator.apache.org Tue Jun 5 16:49:44 2012 Return-Path: X-Original-To: apmail-incubator-celix-dev-archive@minotaur.apache.org Delivered-To: apmail-incubator-celix-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A6944C89C for ; Tue, 5 Jun 2012 16:49:44 +0000 (UTC) Received: (qmail 65995 invoked by uid 500); 5 Jun 2012 16:49:44 -0000 Delivered-To: apmail-incubator-celix-dev-archive@incubator.apache.org Received: (qmail 65966 invoked by uid 500); 5 Jun 2012 16:49:44 -0000 Mailing-List: contact celix-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: celix-dev@incubator.apache.org Delivered-To: mailing list celix-dev@incubator.apache.org Received: (qmail 65957 invoked by uid 99); 5 Jun 2012 16:49:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Jun 2012 16:49:44 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of s.zelzer@dkfz-heidelberg.de designates 192.54.49.101 as permitted sender) Received: from [192.54.49.101] (HELO mx-ext.inet.dkfz-heidelberg.de) (192.54.49.101) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Jun 2012 16:49:36 +0000 Received: from [192.168.11.4] (HSI-KBW-46-223-218-16.hsi.kabel-badenwuerttemberg.de [46.223.218.16]) (authenticated bits=0) by mx-ext.inet.dkfz-heidelberg.de (8.13.8/8.13.8/smtpin) with ESMTP id q55Gn2Ue011593 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 5 Jun 2012 18:49:03 +0200 Message-ID: <4FCE387D.9030304@dkfz-heidelberg.de> Date: Tue, 05 Jun 2012 18:49:01 +0200 From: Sascha Zelzer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: celix-dev@incubator.apache.org Subject: Re: [Native-OSGi] OSGi API: Allocated memory ownership References: <4FC8C52A.7000604@dkfz-heidelberg.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-3.0 (mx-ext.inet.dkfz-heidelberg.de [192.54.49.101]); Tue, 05 Jun 2012 18:49:05 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on mx-ext.inet.dkfz-heidelberg.de X-Old-Spam-Status: No, score=-101.4 required=5.0 tests=ALL_TRUSTED,LOCAL_AUTH_GREY autolearn=disabled version=3.2.5 Hi, On 06/04/2012 12:00 PM, Alexander Broekhuis wrote: >> I'm not a C - expert, but I think the only viable solution is to >> accurately document the lifetime and ownership of pointers. >> > Well I think this is not a C specific problem, the same applies to C++ I > guess. Someone has to free allocated memory. Sure. I meant that I am familiar with a couple of C++ idioms for managing memory (smart pointers, copy constructors, implicitly shared classes, new/delete overloads, custom allocators etc.) but I am not accustomed with possibly existing idioms in C. >> Memory which is to be owned by the caller (user) could probably be >> allocated and deleted by using special functions (hooks) provided by each >> bundle which use implementation specific routines for memory management. >> Otherwise we just state that the returned memory belongs to the caller. > This is the more interesting part of the problem. > > How would such hooks look like, and doesn't this making bundles awkward? > > A generic header for this could look like this: > memory_hooks.h: > int mem_alloc(void *handle, size_t size, void **block); > int mem_calloc(void *handle, size_t num, size_t size, void **block); > int mem_realloc(void *handle, void *block, size_t size, void **newblock); > int mem_free(void *handle, void *block); > > These function are basically the "normal" C memory functions, does C++ have > different/additional ones? > I've added a *handle to all of them to be able to pass some "instance" to > it. For example, APR would need a memory pool, which can be a member of the > instance type/struct. > > How does the framework get the handle? One additional method could work: > int mem_createHandle(void **handle); > > For APR: > mem_alloc -> use apr_palloc(handle->pool, size) > mem_calloc -> use apr_pcalloc(handle->pool, size) > mem_realloc -> no alternative in APR, implement a solution like [1] > mem_free -> not implemented, the user has to clear the pool at some time > > Looking at it like this, I just realize this solution has a huge drawback > when used in combination with APR. For different calls different pools are > needed. Using this model, there is only one pool which is used for every > allocation. Basically this means the user is not in control of the lifetime > of memory. > The only solution I can think of is to have the user supply the pool on > which the memory has to be allocated, but we don't want this on our API. > > A possibility would be to supply the handle (instead of the pool) which is > used by the memory hooks. For example: > > int bundle_getEntry(bundle, handle, char *name, char **entry) > > Instead of letting the framework use handle, handle is simply passed on the > the memory hooks and used in a way the caller wants. This also eliminates > the need for the createHandle function in the header file. The user can > create one when needed. In case of APR, this handle probably can directly > be the pool. > > I hope this still makes sense... > > [1]: http://marc.info/?l=apr-dev&m=109065999530290&w=2 > Yes, it is getting complicated... ;-) Maybe another possibility which avoids the introduction of handles in the API is to pass the function pointer of the API function being called to the memory handling functions. E.g. int bundle_getEntry(bundle, char *name, char **entry) { bundle_mem_alloc(bundle_getEntry, , entry); ... } So bundles could use different pools for different API calls. Maybe that kind of flexibility is enough... If there is no bundle specific "mem_alloc" function, the framework could fall back to usual malloc/free calls. Does that make sense? - Sascha