[PATCH] counter type change
Harrie Hazewinkel <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <10110987.1021046787@localhost> |
Hi, (repost due to using a non-subscribed address) I have attached a patch for the kannel code which make the internal type of the counter object an 'unsigned long'. The advantage is that it wraps by itself and we have 1 bit more in the value of the counter (32 bits). :-)) I noticed also that sometimes the value of the counter returned by the accessor-functions used already 'unsigned long'. I have also added a 'counter_increase_with' value for usage to increase counters by more then 1. This is when one would count for instance the total bytes of the messages sent. Besdies I have a question; why is in most cases the counter value retrieved and stored elsewhere. One could directly get the value of the counter by just keeping the reference to it in those placed where now this value is 'copied'. cheers, Harrie Internet Management Consulting tel: +39-3474932300 / +31-625357135 mailto:[email protected] http ://www.mod-snmp.com/
counter.patch
(application/octet-stream, 9.4 KB)
diff -ru ../gateway-1.1.6-orig/gw/shared.c ./gw/shared.c
--- ../gateway-1.1.6-orig/gw/shared.c Mon Jan 28 01:00:36 2002
+++ ./gw/shared.c Tue May 7 10:44:04 2002
@@ -285,11 +285,12 @@
List *sms_split(Msg *orig, Octstr *header, Octstr *footer,
Octstr *nonlast_suffix, Octstr *split_chars,
- int catenate, int msg_sequence, int max_messages,
- int max_octets)
+ int catenate, unsigned long msg_sequence,
+ int max_messages, int max_octets)
{
long max_part_len, udh_len, hf_len, nlsuf_len;
- long total_messages, msgno, last;
+ unsigned long total_messages, msgno;
+ long last;
List *list;
Msg *part, *temp;
diff -ru ../gateway-1.1.6-orig/gw/shared.h ./gw/shared.h
--- ../gateway-1.1.6-orig/gw/shared.h Fri Dec 21 02:57:14 2001
+++ ./gw/shared.h Tue May 7 10:42:01 2002
@@ -100,7 +100,7 @@
*/
List *sms_split(Msg *orig, Octstr *header, Octstr *footer,
Octstr *nonlast_suffix, Octstr *split_chars, int catenate,
- int msg_sequence, int max_messages, int max_octets);
+ unsigned long msg_sequence, int max_messages, int max_octets);
#endif
diff -ru ../gateway-1.1.6-orig/gw/smsbox.c ./gw/smsbox.c
--- ../gateway-1.1.6-orig/gw/smsbox.c Thu Mar 21 04:54:32 2002
+++ ./gw/smsbox.c Tue May 7 10:41:07 2002
@@ -129,7 +129,8 @@
{
int max_msgs;
Octstr *header, *footer, *suffix, *split_chars;
- int catenate, msg_sequence;
+ int catenate;
+ unsigned long msg_sequence;
List *list;
Msg *part;
diff -ru ../gateway-1.1.6-orig/gw/smscconn.h ./gw/smscconn.h
--- ../gateway-1.1.6-orig/gw/smscconn.h Fri Aug 31 21:54:47 2001
+++ ./gw/smscconn.h Tue May 7 10:45:15 2002
@@ -108,9 +108,9 @@
int status; /* see enumeration, below */
int killed; /* if we are killed, why */
int is_stopped; /* is connection currently in stopped state? */
- long received; /* total number */
- long sent; /* total number */
- long failed; /* total number */
+ unsigned long received; /* total number */
+ unsigned long sent; /* total number */
+ unsigned long failed; /* total number */
long queued; /* set our internal outgoing queue length */
long online; /* in seconds */
int load; /* subjective value 'how loaded we are' for
diff -ru ../gateway-1.1.6-orig/gw/wapbox.c ./gw/wapbox.c
--- ../gateway-1.1.6-orig/gw/wapbox.c Sun Jan 27 23:57:15 2002
+++ ./gw/wapbox.c Tue May 7 10:48:13 2002
@@ -354,7 +354,7 @@
List *sms_datagrams;
long max_msgs,
msg_len;
- static long msg_sequence = 0L; /* Used only by this function */
+ static unsigned long msg_sequence = 0L; /* Used only by this function */
gw_assert(dgram);
sms_datagrams = NULL;
diff -ru ../gateway-1.1.6-orig/gwlib/counter.c ./gwlib/counter.c
--- ../gateway-1.1.6-orig/gwlib/counter.c Fri Mar 16 11:43:29 2001
+++ ./gwlib/counter.c Tue May 7 10:21:06 2002
@@ -4,6 +4,11 @@
* This file implements the Counter objects declared in counter.h.
*
* Lars Wirzenius.
+ *
+ * Changed the counter type 'long' into 'unsigned long' so it wraps
+ * by itself. Just keep increasing it.
+ * Also added a counter_increase_with function.
+ * [email protected]
*/
#include <limits.h>
@@ -13,7 +18,7 @@
struct Counter
{
Mutex *lock;
- long n;
+ unsigned long n;
};
Counter *counter_create(void)
@@ -33,33 +38,41 @@
gw_free(counter);
}
-long counter_increase(Counter *counter)
+unsigned long counter_increase(Counter *counter)
{
- long ret;
+ unsigned long ret;
mutex_lock(counter->lock);
ret = counter->n;
- if (counter->n == LONG_MAX)
- counter->n = 0;
- else
- ++counter->n;
+ ++counter->n;
mutex_unlock(counter->lock);
return ret;
}
-long counter_value(Counter *counter)
+unsigned long counter_increase_with(Counter *counter, unsigned long value)
{
- long ret;
+ unsigned long ret;
mutex_lock(counter->lock);
ret = counter->n;
+ counter->n += value;
mutex_unlock(counter->lock);
return ret;
}
-long counter_decrease(Counter *counter)
+unsigned long counter_value(Counter *counter)
{
- long ret;
+ unsigned long ret;
+
+ mutex_lock(counter->lock);
+ ret = counter->n;
+ mutex_unlock(counter->lock);
+ return ret;
+}
+
+unsigned long counter_decrease(Counter *counter)
+{
+ unsigned long ret;
mutex_lock(counter->lock);
ret = counter->n;
@@ -69,9 +82,9 @@
return ret;
}
-long counter_set(Counter *counter, long n)
+unsigned long counter_set(Counter *counter, unsigned long n)
{
- long ret;
+ unsigned long ret;
mutex_lock(counter->lock);
ret = counter->n;
diff -ru ../gateway-1.1.6-orig/gwlib/counter.h ./gwlib/counter.h
--- ../gateway-1.1.6-orig/gwlib/counter.h Mon Aug 21 15:15:36 2000
+++ ./gwlib/counter.h Tue May 7 10:21:06 2002
@@ -7,6 +7,11 @@
* if it reaches LONG_MAX, it wraps around to zero (_NOT_ LONG_MIN).
*
* Lars Wirzenius.
+ *
+ * Changed the counter type 'long' into 'unsigned long' so it wraps
+ * by itself. Just keep increasing it.
+ * Also added a counter_increase_with function.
+ * [email protected]
*/
@@ -22,15 +27,18 @@
void counter_destroy(Counter *counter);
/* return the current value of the counter and increase counter by one */
-long counter_increase(Counter *counter);
+unsigned long counter_increase(Counter *counter);
+
+/* return the current value of the counter and increase counter by value */
+unsigned long counter_increase_with(Counter *counter, unsigned long value);
/* return the current value of the counter */
-long counter_value(Counter *counter);
+unsigned long counter_value(Counter *counter);
/* return the current value of the counter and decrease counter by one */
-long counter_decrease(Counter *counter);
+unsigned long counter_decrease(Counter *counter);
/* return the current value of the counter and set it to the supplied value */
-long counter_set(Counter *, long);
+unsigned long counter_set(Counter *, unsigned long);
#endif
diff -ru ../gateway-1.1.6-orig/test/drive_smpp.c ./test/drive_smpp.c
--- ../gateway-1.1.6-orig/test/drive_smpp.c Fri Oct 19 03:45:28 2001
+++ ./test/drive_smpp.c Tue May 7 10:32:02 2002
@@ -102,7 +102,7 @@
static SMPP_PDU *handle_submit_sm(ESME *esme, SMPP_PDU *pdu)
{
SMPP_PDU *resp;
- long id;
+ unsigned long id;
debug("test.smpp", 0, "submit_sm: short_message = <%s>",
octstr_get_cstr(pdu->u.submit_sm.short_message));
@@ -195,7 +195,7 @@
ESME *esme;
Octstr *os;
SMPP_PDU *pdu;
- long id;
+ unsigned long id;
esme = arg;
@@ -304,7 +304,7 @@
Msg *msg;
Octstr *os;
Octstr *reply_msg;
- long count;
+ unsigned long count;
msg = msg_create(sms);
msg->sms.sender = octstr_create("123");
diff -ru ../gateway-1.1.6-orig/test/test_http.c ./test/test_http.c
--- ../gateway-1.1.6-orig/test/test_http.c Fri Jan 25 12:49:34 2002
+++ ./test/test_http.c Tue May 7 10:32:41 2002
@@ -95,7 +95,8 @@
static void client_thread(void *arg)
{
List *reqh;
- long i, succeeded, failed;
+ unsigned long i;
+ long succeeded, failed;
HTTPCaller *caller;
char buf[1024];
long in_queue;
diff -ru ../gateway-1.1.6-orig/test/test_ppg.c ./test/test_ppg.c
--- ../gateway-1.1.6-orig/test/test_ppg.c Thu Mar 7 00:34:21 2002
+++ ./test/test_ppg.c Tue May 7 10:33:21 2002
@@ -512,7 +512,8 @@
static void push_thread(void *arg)
{
HTTPCaller *caller;
- long succeeded, failed, in_queue, i;
+ long succeeded, failed, in_queue;
+ unsigned long i;
caller = arg;
succeeded = 0;
diff -ru ../gateway-1.1.6-orig/wap/wsp_session.c ./wap/wsp_session.c
--- ../gateway-1.1.6-orig/wap/wsp_session.c Mon Apr 23 18:52:30 2001
+++ ./wap/wsp_session.c Tue May 7 10:22:56 2002
@@ -84,7 +84,7 @@
static void push_machine_destroy(void *p);
static char *state_name(WSPState state);
-static long next_wsp_session_id(void);
+static unsigned long next_wsp_session_id(void);
static List *make_capabilities_reply(WSPMachine *m);
static Octstr *make_connectreply_pdu(WSPMachine *m);
@@ -685,7 +685,7 @@
}
-static long next_wsp_session_id(void) {
+static unsigned long next_wsp_session_id(void) {
return counter_increase(session_id_counter);
}
diff -ru ../gateway-1.1.6-orig/wap/wtls.h ./wap/wtls.h
--- ../gateway-1.1.6-orig/wap/wtls.h Fri Jul 6 09:54:51 2001
+++ ./wap/wtls.h Tue May 7 10:25:09 2002
@@ -33,7 +33,7 @@
* every separate type.
*/
struct WTLSMachine {
- long mid;
+ unsigned long mid;
#define ENUM(name) serv_states name;
#define ADDRTUPLE(name) WAPAddrTuple *name;
#define INTEGER(name) int name;
diff -ru ../gateway-1.1.6-orig/wap/wtp_init.h ./wap/wtp_init.h
--- ../gateway-1.1.6-orig/wap/wtp_init.h Mon Nov 20 20:55:54 2000
+++ ./wap/wtp_init.h Tue May 7 10:26:38 2002
@@ -31,7 +31,7 @@
* every separate type.
*/
typedef struct WTPInitMachine {
- long mid;
+ unsigned long mid;
#define INTEGER(name) int name;
#define EVENT(name) WAPEvent *name;
#define TIMER(name) Timer *name;
diff -ru ../gateway-1.1.6-orig/wap/wtp_resp.h ./wap/wtp_resp.h
--- ../gateway-1.1.6-orig/wap/wtp_resp.h Mon Nov 20 20:55:54 2000
+++ ./wap/wtp_resp.h Tue May 7 10:27:27 2002
@@ -32,7 +32,7 @@
* every separate type.
*/
struct WTPRespMachine {
- long mid;
+ unsigned long mid;
#define INTEGER(name) int name;
#define TIMER(name) Timer *name;
#define ADDRTUPLE(name) WAPAddrTuple *name;