* lib/queue.c, lib/queue.h: New generic queue data structure

This commit is contained in:
tedbullock 2007-07-16 04:57:21 +00:00
parent 658e4d646f
commit 938c4ed349
4 ha cambiato i file con 199 aggiunte e 2 eliminazioni

Vedi File

@ -1,3 +1,7 @@
2007-07-15 Ted Bullock <tbullock@canada.com>
* lib/queue.c, lib/queue.h: New generic queue data structure
2007-07-15 Ted Bullock <tbullock@canada.com>
* lib/generic_types.h: New File - Migrate various generic types

Vedi File

@ -1,6 +1,7 @@
# what flags you want to pass to the C compiler & linker
AM_CFLAGS = -I$(srcdir)/..
AM_LDFLAGS =
noinst_LIBRARIES = libutil.a
libutil_a_SOURCES = getopt.c getopt.h getopt1.c ssl_writev.c generic_types.h
libutil_a_SOURCES = getopt.c getopt.h getopt1.c ssl_writev.c generic_types.h \
queue.c queue.h

147
httperf/src/lib/queue.c Normal file
Vedi File

@ -0,0 +1,147 @@
/*
* Copyright (C) 2007 Ted Bullock <tbullock@canada.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.
*
* In addition, as a special exception, the copyright holders give permission
* to link the code of this work with the OpenSSL project's "OpenSSL" library
* (or with modified versions of it that use the same license as the "OpenSSL"
* library), and distribute linked combinations including the two. You must
* obey the GNU General Public License in all respects for all of the code
* used other than "OpenSSL". If you modify this file, you may extend this
* exception to your version of the file, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your
* 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <generic_types.h>
#include <queue.h>
#include <httperf.h>
#define Minimum_Wheel_Size 10
struct Queue {
u_long wheel_size;
u_long num_elements;
u_long first_element;
u_long last_element;
Any_Type wheel[]; /* c99 Flexible Array Member */
};
static void
empty_queue(struct Queue *q)
{
q->num_elements = 0;
q->first_element = 1;
q->last_element = 0;
for (u_long i = 0; i < q->wheel_size; i++)
q->wheel[i].uw = 0;
}
struct Queue *
create_queue(u_long size)
{
struct Queue *q;
if (size < Minimum_Wheel_Size)
size = Minimum_Wheel_Size;
/*
* Again, we are using c99 Flexible Array Members so we can do this :)
*/
q = malloc(sizeof(struct Queue) + sizeof(Any_Type) * size);
if (!q) {
fprintf(stderr, "%s.create_queue: %s\n", prog_name,
strerror(errno));
return 0;
}
q->wheel_size = size;
empty_queue(q);
return q;
}
int
is_queue_empty(struct Queue *q)
{
return q->num_elements == 0;
}
int
is_queue_full(struct Queue *q)
{
return q->num_elements == q->wheel_size;
}
void
free_queue(struct Queue *q)
{
if (q != NULL) {
free(q);
}
}
int
enqueue(Any_Type a, struct Queue *q)
{
if (is_queue_full(q))
return 0;
else {
q->num_elements++;
if (++q->last_element == q->wheel_size) {
q->last_element = 0;
}
q->wheel[q->last_element] = a;
}
}
void
dequeue(struct Queue *q)
{
q->num_elements--;
if (++q->first_element == q->wheel_size)
q->first_element = 0;
}
Any_Type
get_front(struct Queue *q)
{
return q->wheel[q->first_element];
}
Any_Type
get_front_and_dequeue(struct Queue * q)
{
Any_Type a = get_front(q);
dequeue(q);
return a;
}

45
httperf/src/lib/queue.h Normal file
Vedi File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2007 Ted Bullock <tbullock@canada.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.
*
* In addition, as a special exception, the copyright holders give permission
* to link the code of this work with the OpenSSL project's "OpenSSL" library
* (or with modified versions of it that use the same license as the "OpenSSL"
* library), and distribute linked combinations including the two. You must
* obey the GNU General Public License in all respects for all of the code
* used other than "OpenSSL". If you modify this file, you may extend this
* exception to your version of the file, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your
* 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef queue_h
#define queue_h
struct Queue;
struct Queue *create_queue(u_long);
int is_queue_empty(struct Queue *);
int is_queue_full(struct Queue *);
void free_queue(struct Queue *);
int enqueue(Any_Type, struct Queue *);
Any_Type get_front(struct Queue *);
void dequeue(struct Queue *);
Any_Type get_front_and_dequeue(struct Queue *);
#endif /* queue_h */