You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@axis.apache.org by "Samisa Abeysinghe (JIRA)" <ji...@apache.org> on 2006/09/06 11:32:22 UTC
[jira] Created: (AXIS2C-254) ops structs should be
statically-allocated and shared
ops structs should be statically-allocated and shared
-----------------------------------------------------
Key: AXIS2C-254
URL: http://issues.apache.org/jira/browse/AXIS2C-254
Project: Axis2-C
Issue Type: Improvement
Affects Versions: Current (Nightly)
Reporter: Samisa Abeysinghe
At the moment, when objects have an ops field pointing to an *_ops
struct with function pointers, you are dynamically allocating a separate
copy of the ops struct for each instance of the class. The whole point
of factoring out the function pointers into a separate ops struct is so
that a single statically-allocated ops struct can be shared between all
instances of a given class.
Thus the pattern is supposed to be something like this:
/* bar.h */
typedef struct bar bar_t;
typedef struct bar_ops {
void destroy(bar_t *);
void x(bar_t *);
} bar_ops_t;
struct bar {
const bar_ops_t *ops;
};
#define BAR_X(b) ((f)->ops->x(b))
bar_t *bar1_create();
bar_t *bar2_create();
/* bar1.c */
typedef struct bar1 {
bar_t base;
int some_private_data;
} bar1_t;
static void destroy1(bar_t *);
static void x1(bar_t *);
static const bar_ops_t bar1_ops = {
destroy1,
x1
};
bar_t *bar1_create()
{
bar1_t *b1 = malloc(sizeof(bar1_t));
b1->ops = &bar1_ops;
b1->some_private_date = 42;
return &b1->base;
}
static
void destroy1(bar_t *bar)
{
free(bar)
}
Points to note:
- the ops member of struct bar is a const pointer
- bar1_ops is both static and const
- the method implementations like x1 are all declared static (although
the x1 function will be called from outside bar1.c, it is only called
via the function pointer, so its name does not need to be exported)
- the create/destroy methods do a single allocation/deallocation
Fixing this will cut the number of allocations that you do in half!
(based on comments by James)
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-c-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-c-dev-help@ws.apache.org
[jira] Updated: (AXIS2C-254) ops structs should be
statically-allocated and shared
Posted by "Samisa Abeysinghe (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/AXIS2C-254?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Samisa Abeysinghe updated AXIS2C-254:
-------------------------------------
Fix Version/s: 1.0.0
> ops structs should be statically-allocated and shared
> -----------------------------------------------------
>
> Key: AXIS2C-254
> URL: https://issues.apache.org/jira/browse/AXIS2C-254
> Project: Axis2-C
> Issue Type: Improvement
> Affects Versions: Current (Nightly)
> Reporter: Samisa Abeysinghe
> Fix For: 1.0.0
>
>
> At the moment, when objects have an ops field pointing to an *_ops
> struct with function pointers, you are dynamically allocating a separate
> copy of the ops struct for each instance of the class. The whole point
> of factoring out the function pointers into a separate ops struct is so
> that a single statically-allocated ops struct can be shared between all
> instances of a given class.
> Thus the pattern is supposed to be something like this:
> /* bar.h */
> typedef struct bar bar_t;
> typedef struct bar_ops {
> void destroy(bar_t *);
> void x(bar_t *);
> } bar_ops_t;
> struct bar {
> const bar_ops_t *ops;
> };
> #define BAR_X(b) ((f)->ops->x(b))
> bar_t *bar1_create();
> bar_t *bar2_create();
> /* bar1.c */
> typedef struct bar1 {
> bar_t base;
> int some_private_data;
> } bar1_t;
> static void destroy1(bar_t *);
> static void x1(bar_t *);
> static const bar_ops_t bar1_ops = {
> destroy1,
> x1
> };
> bar_t *bar1_create()
> {
> bar1_t *b1 = malloc(sizeof(bar1_t));
> b1->ops = &bar1_ops;
> b1->some_private_date = 42;
> return &b1->base;
> }
> static
> void destroy1(bar_t *bar)
> {
> free(bar)
> }
> Points to note:
> - the ops member of struct bar is a const pointer
> - bar1_ops is both static and const
> - the method implementations like x1 are all declared static (although
> the x1 function will be called from outside bar1.c, it is only called
> via the function pointer, so its name does not need to be exported)
> - the create/destroy methods do a single allocation/deallocation
> Fixing this will cut the number of allocations that you do in half!
> (based on comments by James)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-c-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-c-dev-help@ws.apache.org
[jira] Resolved: (AXIS2C-254) ops structs should be
statically-allocated and shared
Posted by "Samisa Abeysinghe (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/AXIS2C-254?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Samisa Abeysinghe resolved AXIS2C-254.
--------------------------------------
Resolution: Duplicate
Same as AXIS2C-473
> ops structs should be statically-allocated and shared
> -----------------------------------------------------
>
> Key: AXIS2C-254
> URL: https://issues.apache.org/jira/browse/AXIS2C-254
> Project: Axis2-C
> Issue Type: Improvement
> Affects Versions: Current (Nightly)
> Reporter: Samisa Abeysinghe
> Fix For: 1.0.0
>
>
> At the moment, when objects have an ops field pointing to an *_ops
> struct with function pointers, you are dynamically allocating a separate
> copy of the ops struct for each instance of the class. The whole point
> of factoring out the function pointers into a separate ops struct is so
> that a single statically-allocated ops struct can be shared between all
> instances of a given class.
> Thus the pattern is supposed to be something like this:
> /* bar.h */
> typedef struct bar bar_t;
> typedef struct bar_ops {
> void destroy(bar_t *);
> void x(bar_t *);
> } bar_ops_t;
> struct bar {
> const bar_ops_t *ops;
> };
> #define BAR_X(b) ((f)->ops->x(b))
> bar_t *bar1_create();
> bar_t *bar2_create();
> /* bar1.c */
> typedef struct bar1 {
> bar_t base;
> int some_private_data;
> } bar1_t;
> static void destroy1(bar_t *);
> static void x1(bar_t *);
> static const bar_ops_t bar1_ops = {
> destroy1,
> x1
> };
> bar_t *bar1_create()
> {
> bar1_t *b1 = malloc(sizeof(bar1_t));
> b1->ops = &bar1_ops;
> b1->some_private_date = 42;
> return &b1->base;
> }
> static
> void destroy1(bar_t *bar)
> {
> free(bar)
> }
> Points to note:
> - the ops member of struct bar is a const pointer
> - bar1_ops is both static and const
> - the method implementations like x1 are all declared static (although
> the x1 function will be called from outside bar1.c, it is only called
> via the function pointer, so its name does not need to be exported)
> - the create/destroy methods do a single allocation/deallocation
> Fixing this will cut the number of allocations that you do in half!
> (based on comments by James)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-c-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-c-dev-help@ws.apache.org