1 /*
2 httperf -- a tool for measuring web server performance
3 Copyright 2000-2007 Hewlett-Packard Company
4
5 This file is part of httperf, a web server performance measurment
6 tool.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 In addition, as a special exception, the copyright holders give
14 permission to link the code of this work with the OpenSSL project's
15 "OpenSSL" library (or with modified versions of it that use the same
16 license as the "OpenSSL" library), and distribute linked combinations
17 including the two. You must obey the GNU General Public License in
18 all respects for all of the code used other than "OpenSSL". If you
19 modify this file, you may extend this exception to your version of the
20 file, but you are not obligated to do so. If you do not wish to do
21 so, delete this exception statement from your version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 02110-1301, USA
32 */
33
34 #include "config.h"
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <generic_types.h>
43
44 #include <object.h>
45 #include <timer.h>
46 #include <httperf.h>
47 #include <call.h>
48 #include <conn.h>
49 #include <localevent.h>
50 #include <sess.h>
51
52 #define ALIGN(s) (((s) + sizeof (double) - 1) & ~(sizeof (double) - 1))
53
54 static size_t type_size[OBJ_NUM_TYPES] =
55 {
56 ALIGN (sizeof (Conn)),
57 ALIGN (sizeof (Call)),
58 ALIGN (sizeof (Sess))
59 };
60
61 struct free_list_el
62 {
63 struct free_list_el *next;
64 };
65
66 static struct free_list_el *free_list[OBJ_NUM_TYPES];
67
68 static void
69 object_destroy (Object *obj)
70 {
71 Object_Type type = obj->type;
72 struct free_list_el *el;
73 Event_Type event = EV_NULL;
74 Any_Type arg;
75
76 switch (type)
77 {
78 case OBJ_CALL:
79 call_deinit ((Call *) obj);
80 event = EV_CALL_DESTROYED;
81 break;
82
83 case OBJ_CONN:
84 conn_deinit ((Conn *) obj);
85 event = EV_CONN_DESTROYED;
86 break;
87
88 case OBJ_SESS:
89 sess_deinit ((Sess *) obj);
event_register_handler(Event_Type et,Event_Handler handler,Any_Type arg)90 event = EV_SESS_DESTROYED;
91 break;
92
93 default:
94 assert (0);
95 break;
96 }
97 arg.l = 0;
98 event_signal (event, obj, arg);
99
100 /* Each object must be at least the size and alignment of "struct
101 free_list_el". Malloc takes care of returning properly aligned
102 objects. */
103 el = (struct free_list_el *) obj;
104 el->next = free_list[type];
105 free_list[type] = el;
106 }
107
108 size_t
event_signal(Event_Type type,Object * obj,Any_Type arg)109 object_expand (Object_Type type, size_t size)
110 {
111 size_t offset = type_size[type];
112 type_size[type] += ALIGN (size);
113 return offset;
114 }
115
116 Object *
117 object_new (Object_Type type)
118 {
119 struct free_list_el *el;
120 Event_Type event = EV_NULL;
121 size_t obj_size;
122 Any_Type arg;
123 Object *obj;
124
125 obj_size = type_size[type];
126
127 if (free_list[type])
128 {
129 el = free_list[type];
130 free_list[type] = el->next;
131 obj = (Object *) el;
132 }
133 else
134 {
135 obj = malloc (obj_size);
136 if (!obj)
137 {
138 fprintf (stderr, "%s.object_new: %s\n", prog_name, strerror (errno));
139 return 0;
140 }
141 }
142 memset (obj, 0, obj_size);
143 obj->ref_count = 1;
144 obj->type = type;
145 switch (type)
146 {
147 case OBJ_CALL:
148 call_init ((Call *) obj);
149 event = EV_CALL_NEW;
150 break;
151
152 case OBJ_CONN:
153 conn_init ((Conn *) obj);
154 event = EV_CONN_NEW;
155 break;
156
157 case OBJ_SESS:
158 sess_init ((Sess *) obj);
159 event = EV_SESS_NEW;
160 break;
161
162 default:
163 panic ("object_new: bad object type %d\n", type);
164 break;
165 }
166 arg.l = 0;
167 event_signal (event, obj, arg);
168 return obj;
169 }
170
171 void
172 object_dec_ref (Object *obj)
173 {
174 assert (obj->ref_count > 0);
175
176 if (--obj->ref_count == 0)
177 object_destroy (obj);
178 }
179