"Fossies" - the Fresh Open Source Software Archive

Member "httperf-0.9.0/src/object.c" (7 Apr 2007, 4252 Bytes) of package /linux/www/old/httperf-0.9.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "object.c" see the Fossies "Dox" file reference documentation.

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