File: object.c

package info (click to toggle)
httperf 0.8-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 736 kB
  • ctags: 816
  • sloc: ansic: 6,665; sh: 2,473; makefile: 228
file content (164 lines) | stat: -rw-r--r-- 3,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
    httperf -- a tool for measuring web server performance
    Copyright (C) 2000  Hewlett-Packard Company
    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>

    This file is part of httperf, a web server performance measurment
    tool.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <httperf.h>
#include <call.h>
#include <conn.h>
#include <event.h>
#include <object.h>
#include <sess.h>

#define ALIGN(s)	(((s) + sizeof (double) - 1) & ~(sizeof (double) - 1))

static size_t type_size[OBJ_NUM_TYPES] =
  {
    ALIGN (sizeof (Conn)),
    ALIGN (sizeof (Call)),
    ALIGN (sizeof (Sess))
  };

struct free_list_el
  {
    struct free_list_el *next;
  };

static struct free_list_el *free_list[OBJ_NUM_TYPES];

static void
object_destroy (Object *obj)
{
  Object_Type type = obj->type;
  struct free_list_el *el;
  Event_Type event = 0;
  Any_Type arg;

  switch (type)
    {
    case OBJ_CALL:
      call_deinit ((Call *) obj);
      event = EV_CALL_DESTROYED;
      break;

    case OBJ_CONN:
      conn_deinit ((Conn *) obj);
      event = EV_CONN_DESTROYED;
      break;

    case OBJ_SESS:
      sess_deinit ((Sess *) obj);
      event = EV_SESS_DESTROYED;
      break;

    default:
      assert (0);
      break;
    }
  arg.l = 0;
  event_signal (event, obj, arg);

  /* Each object must be at least the size and alignment of "struct
     free_list_el".  Malloc takes care of returning properly aligned
     objects.  */
  el = (struct free_list_el *) obj;
  el->next = free_list[type];
  free_list[type] = el;
}

size_t
object_expand (Object_Type type, size_t size)
{
  size_t offset = type_size[type];
  type_size[type] += ALIGN (size);
  return offset;
}

Object *
object_new (Object_Type type)
{
  struct free_list_el *el;
  Event_Type event = 0;
  size_t obj_size;
  Any_Type arg;
  Object *obj;

  obj_size = type_size[type];

  if (free_list[type])
    {
      el = free_list[type];
      free_list[type] = el->next;
      obj = (Object *) el;
    }
  else
    {
      obj = malloc (obj_size);
      if (!obj)
	{
	  fprintf (stderr, "%s.object_new: %s\n", prog_name, strerror (errno));
	  return 0;
	}
    }
  memset (obj, 0, obj_size);
  obj->ref_count = 1;
  obj->type = type;
  switch (type)
    {
    case OBJ_CALL:
      call_init ((Call *) obj);
      event = EV_CALL_NEW;
      break;

    case OBJ_CONN:
      conn_init ((Conn *) obj);
      event = EV_CONN_NEW;
      break;

    case OBJ_SESS:
      sess_init ((Sess *) obj);
      event = EV_SESS_NEW;
      break;

    default:
      panic ("object_new: bad object type %d\n", type);
      break;
    }
  arg.l = 0;
  event_signal (event, obj, arg);
  return obj;
}

void
object_dec_ref (Object *obj)
{
  assert (obj->ref_count > 0);

  if (--obj->ref_count == 0)
    object_destroy (obj);
}