design_pattern_for_c  V 1.00
prototype_manager.c
Go to the documentation of this file.
1 
5 #include <stdlib.h>
6 #include "dp_util.h"
7 #include "prototype_factory.h"
8 
9 /*************
10  * public define
11 *************/
15  pthread_mutex_t *lock;
17 };
18 
25  pthread_mutex_t *lock;
26 };
27 
28 #define prototype_push(this, data) dputil_list_push((DPUtilList)this, (DPUtilListData)data);
29 #define prototype_pull(this, data) dputil_list_pull((DPUtilList)this, (DPUtilListData)data);
30 #define prototype_pop(this) (PrototypeFactory)dputil_list_pop((DPUtilList)this);
31 
32 #define PROTOTYPE_LOCK(this) DPUTIL_LOCK(this->lock);
33 #define PROTOTYPE_UNLOCK DPUTIL_UNLOCK
34 
36  PrototypeManager instance = calloc(1, sizeof(*instance));
37  if(!instance) {
38  DEBUG_ERRPRINT("Failed to create instance\n");
39  return NULL;
40  }
41 
42  if(is_threadsafe) {
43  instance->lock = malloc(sizeof(*(instance->lock)));
44  if(!instance->lock) {
45  DEBUG_ERRPRINT("Fai|led to create lock instance\n");
46  free(instance);
47  return NULL;
48  }
49  pthread_mutex_init(instance->lock, NULL);
50  }
51 
52  return instance;
53 }
54 
56  pthread_mutex_t *lock=NULL;
57  if(!this) {
58  return;
59  }
60 
61 PROTOTYPE_LOCK(this)
62  lock=this->lock;
63  PrototypeFactory factory = prototype_pop(this);
64  while(factory) {
66  free(factory);
67  factory = prototype_pop(this);
68  }
69  free(this);
71  free(lock);
72 }
73 
74 PrototypeFactory prototype_register(PrototypeManager this, void * base, size_t base_length, prototype_factory_method_t * factory_method) {
75  PrototypeFactory factory = NULL;
76  if(!this || !base || base_length==0) {
77  return NULL;
78  }
79 
80 PROTOTYPE_LOCK(this)
81  factory = calloc(1, sizeof(*factory));
82  if(!factory) {
83  DEBUG_ERRPRINT("Fai|led to create PrototypeFactory\n");
84  goto end;
85  }
86 
87  factory->instance = prototype_factory_instance_new(base, base_length, factory_method);
88  if(!factory->instance) {
89  DEBUG_ERRPRINT("Fai|led to create PrototypeFactory instance\n");
90  free(factory);
91  factory=NULL;
92  goto end;
93  }
94 
95  factory->lock=this->lock;
96  prototype_push(this, factory);
97 end:
99  return factory;
100 }
101 
103  if(!this || !factory) {
104  return;
105  }
106 
107 PROTOTYPE_LOCK(this)
108  prototype_pull(this, factory);
111  free(factory);
112 }
113 
115  if(!this) {
116  return NULL;
117  }
118 
119  void * data;
120 PROTOTYPE_LOCK(this)
121  data = prototype_factory_instance_clone_data(this->instance);
123  return data;
124 }
125 
126 void prototype_free(PrototypeFactory this, void * cloned_data) {
127  if(!this || !cloned_data) {
128  return;
129  }
130 
131 PROTOTYPE_LOCK(this)
132  prototype_factory_instance_free_data(this->instance, cloned_data);
134 }
PrototypeFactory methods interface definition, to set prototype_register.
pthread_mutex_t * lock
PrototypeFactory tail
PrototypeManager prototype_manager_new(int is_threadsafe)
Create PrototypeManager class.
pthread_mutex_t * lock
Utility headers
PrototypeFactoryInstance instance
void prototype_unregister(PrototypeManager this, PrototypeFactory factory)
Unregister PrototypeFactory class.
PrototypeManager class member definition, defined in prototype_manager.c.
void prototype_factory_instance_free(PrototypeFactoryInstance this)
free
#define prototype_push(this, data)
void(* free)(EventInstance this)
Definition: event_thread.c:39
void prototype_manager_free(PrototypeManager this)
free class handle
prototype_factory_instance, PrototypeFactory class instance definition
void * prototype_factory_instance_clone_data(PrototypeFactoryInstance this)
clone data
PrototypeFactory next
void prototype_factory_instance_free_data(PrototypeFactoryInstance this, void *data)
free data
PrototypeFactoryInstance prototype_factory_instance_new(void *base, size_t base_length, prototype_factory_method_t *factory_method)
new
PrototypeFactory prototype_register(PrototypeManager this, void *base, size_t base_length, prototype_factory_method_t *factory_method)
Register PrototypeFactory class.
This is API for PrototypeFactoryInstance class.
#define DEBUG_ERRPRINT(...)
Definition: dp_debug.h:69
#define prototype_pop(this)
#define PROTOTYPE_LOCK(this)
PrototypeFactory prev
void * prototype_clone(PrototypeFactory this)
Clone base pointer by using PrototypeFactory class.
#define prototype_pull(this, data)
#define PROTOTYPE_UNLOCK
PrototypeFactory methods interface definition, to set prototype_register.
Definition: prototype.h:28
void prototype_free(PrototypeFactory this, void *cloned_data)
Free cloned pointer.
PrototypeFactory head