I'm doing some work putting OpenSSH on an embedded platform, under a
very non-UNIX operating system. This OS doesn't have preemptive
multitasking, and it doesn't have separate address spaces for multiple
"processes"; all instances share the same global data space. Since
various portions of OpenSSL use global data and aren't re-entrant I've
had to play some preprocessor games in the old "errno is a function
call" style, since I want to change as little of the code as possible.
Luckily, OpenSSH really doesn't need that much of OpenSSL so my job is
not quite as arduous as it could be.
Anyway, as part of this I tried to const-ify some parts of OpenSSL to
get a better handle on what global/static data is really constant and
what needs to be handled. One of the things I const-ified was the lhash
library.
Note that this is a special case where you send data to the lhash
library to be stored; I declared that data to be const void* since the
lhash library doesn't modify it. However, to avoid having to cast the
results of the lhash data retrieval functions I cast the data back to
plain void* when it's returned.
This is similar to standard C functions which take a const char*, for
example, and return a char* that points into the string. IMHO this is a
legitimate reason to cast away const, and that the "const" notation on
the arguments to lhash is useful for self-documentation purposes, at
the least.
Hope you find this useful.
Index: lhash/lh_stats.c
===================================================================
RCS file: /global/.builds/psmith/ssh/cvs/openssl/crypto/lhash/lh_stats.c,v
retrieving revision 1.3
diff -u -B -b -r1.3 lh_stats.c
--- lh_stats.c 1999/04/23 22:11:09 1.3
+++ lh_stats.c 2000/05/04 17:46:22
@@ -67,7 +67,7 @@
#ifndef HEADER_BIO_H
-void lh_stats(LHASH *lh, FILE *out)
+void lh_stats(const LHASH *lh, FILE *out)
{
fprintf(out,"num_items = %lu\n",lh->num_items);
fprintf(out,"num_nodes = %u\n",lh->num_nodes);
@@ -93,7 +93,7 @@
#endif
}
-void lh_node_stats(LHASH *lh, FILE *out)
+void lh_node_stats(const LHASH *lh, FILE *out)
{
LHASH_NODE *n;
unsigned int i,num;
@@ -106,7 +106,7 @@
}
}
-void lh_node_usage_stats(LHASH *lh, FILE *out)
+void lh_node_usage_stats(const LHASH *lh, FILE *out)
{
LHASH_NODE *n;
unsigned long num;
@@ -136,7 +136,7 @@
#else
#ifndef NO_FP_API
-void lh_stats(LHASH *lh, FILE *fp)
+void lh_stats(const LHASH *lh, FILE *fp)
{
BIO *bp;
@@ -148,7 +148,7 @@
end:;
}
-void lh_node_stats(LHASH *lh, FILE *fp)
+void lh_node_stats(const LHASH *lh, FILE *fp)
{
BIO *bp;
@@ -160,7 +160,7 @@
end:;
}
-void lh_node_usage_stats(LHASH *lh, FILE *fp)
+void lh_node_usage_stats(const LHASH *lh, FILE *fp)
{
BIO *bp;
@@ -174,7 +174,7 @@
#endif
-void lh_stats_bio(LHASH *lh, BIO *out)
+void lh_stats_bio(const LHASH *lh, BIO *out)
{
char buf[128];
@@ -222,7 +222,7 @@
#endif
}
-void lh_node_stats_bio(LHASH *lh, BIO *out)
+void lh_node_stats_bio(const LHASH *lh, BIO *out)
{
LHASH_NODE *n;
unsigned int i,num;
@@ -237,7 +237,7 @@
}
}
-void lh_node_usage_stats_bio(LHASH *lh, BIO *out)
+void lh_node_usage_stats_bio(const LHASH *lh, BIO *out)
{
LHASH_NODE *n;
unsigned long num;
Index: lhash/lhash.c
===================================================================
RCS file: /global/.builds/psmith/ssh/cvs/openssl/crypto/lhash/lhash.c,v
retrieving revision 1.16
diff -u -B -b -r1.16 lhash.c
--- lhash.c 2000/03/18 15:18:27 1.16
+++ lhash.c 2000/05/04 17:48:09
@@ -109,7 +109,7 @@
static void expand(LHASH *lh);
static void contract(LHASH *lh);
-static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash);
+static LHASH_NODE **getrn(LHASH *lh, const void *data, unsigned long *rhash);
LHASH *lh_new(unsigned long (*h)(), int (*c)())
{
@@ -176,7 +176,7 @@
Free(lh);
}
-void *lh_insert(LHASH *lh, void *data)
+void *lh_insert(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
@@ -207,14 +207,14 @@
}
else /* replace same key */
{
- ret= (*rn)->data;
+ ret= (void *)(*rn)->data;
(*rn)->data=data;
lh->num_replace++;
}
return(ret);
}
-void *lh_delete(LHASH *lh, void *data)
+void *lh_delete(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
@@ -232,7 +232,7 @@
{
nn= *rn;
*rn=nn->next;
- ret=nn->data;
+ ret=(void *)nn->data;
Free(nn);
lh->num_delete++;
}
@@ -245,7 +245,7 @@
return(ret);
}
-void *lh_retrieve(LHASH *lh, void *data)
+void *lh_retrieve(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE **rn;
@@ -261,7 +261,7 @@
}
else
{
- ret= (*rn)->data;
+ ret= (void *)(*rn)->data;
lh->num_retrieve++;
}
return(ret);
@@ -388,7 +388,7 @@
}
}
-static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash)
+static LHASH_NODE **getrn(LHASH *lh, const void *data, unsigned long *rhash)
{
LHASH_NODE **ret,*n1;
unsigned long hash,nn;
@@ -455,7 +455,7 @@
return((ret>>16)^ret);
}
-unsigned long lh_num_items(LHASH *lh)
+unsigned long lh_num_items(const LHASH *lh)
{
return lh ? lh->num_items : 0;
}
Index: lhash/lhash.h
===================================================================
RCS file: /global/.builds/psmith/ssh/cvs/openssl/crypto/lhash/lhash.h,v
retrieving revision 1.7
diff -u -B -b -r1.7 lhash.h
--- lhash.h 2000/05/02 12:15:40 1.7
+++ lhash.h 2000/05/04 17:48:10
@@ -73,7 +73,7 @@
typedef struct lhash_node_st
{
- void *data;
+ const void *data;
struct lhash_node_st *next;
#ifndef NO_HASH_COMP
unsigned long hash;
@@ -118,24 +118,24 @@
LHASH *lh_new(unsigned long (*h)(/* void *a */), int (*c)(/* void *a,void *b */));
void lh_free(LHASH *lh);
-void *lh_insert(LHASH *lh, void *data);
-void *lh_delete(LHASH *lh, void *data);
-void *lh_retrieve(LHASH *lh, void *data);
+void *lh_insert(LHASH *lh, const void *data);
+void *lh_delete(LHASH *lh, const void *data);
+void *lh_retrieve(LHASH *lh, const void *data);
void lh_doall(LHASH *lh, void (*func)(/*void *b*/));
void lh_doall_arg(LHASH *lh, void (*func)(/*void *a,void *b*/),void *arg);
unsigned long lh_strhash(const char *c);
-unsigned long lh_num_items(LHASH *lh);
+unsigned long lh_num_items(const LHASH *lh);
#ifndef NO_FP_API
-void lh_stats(LHASH *lh, FILE *out);
-void lh_node_stats(LHASH *lh, FILE *out);
-void lh_node_usage_stats(LHASH *lh, FILE *out);
+void lh_stats(const LHASH *lh, FILE *out);
+void lh_node_stats(const LHASH *lh, FILE *out);
+void lh_node_usage_stats(const LHASH *lh, FILE *out);
#endif
#ifdef HEADER_BIO_H
-void lh_stats_bio(LHASH *lh, BIO *out);
-void lh_node_stats_bio(LHASH *lh, BIO *out);
-void lh_node_usage_stats_bio(LHASH *lh, BIO *out);
+void lh_stats_bio(const LHASH *lh, BIO *out);
+void lh_node_stats_bio(const LHASH *lh, BIO *out);
+void lh_node_usage_stats_bio(const LHASH *lh, BIO *out);
#endif
#ifdef __cplusplus
}
______________________________________________________________________
OpenSSL Project http://www.openssl.org
Development Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]