Use proper functions for lh_*.

As with sk_*, this. This doesn't fix the function pointer casts. Those
will be done in a follow-up change. Also add a test for lh_*_doall so we
cover both function pointer shapes.

Update-Note: This reworks how LHASH_OF(T) is implemented and also only
pulls in the definitions where used, but LHASH_OF(T) is never used
externally, so I wouldn't expect this to affect things.

Change-Id: I7970ce8c41b8589d6672b71dd03658d0e3bd89a7
Reviewed-on: https://boringssl-review.googlesource.com/c/32119
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/conf/conf.c b/crypto/conf/conf.c
index 4c27ddf..7070ca8 100644
--- a/crypto/conf/conf.c
+++ b/crypto/conf/conf.c
@@ -70,6 +70,8 @@
 #include "../internal.h"
 
 
+DEFINE_LHASH_OF(CONF_VALUE)
+
 struct conf_st {
   LHASH_OF(CONF_VALUE) *data;
 };
diff --git a/crypto/lhash/lhash_test.cc b/crypto/lhash/lhash_test.cc
index a2f61f6..885d3c7 100644
--- a/crypto/lhash/lhash_test.cc
+++ b/crypto/lhash/lhash_test.cc
@@ -28,6 +28,8 @@
 #include <gtest/gtest.h>
 
 
+DEFINE_LHASH_OF(char)
+
 static std::unique_ptr<char[]> RandString(void) {
   unsigned len = 1 + (rand() % 3);
   std::unique_ptr<char[]> ret(new char[len + 1]);
@@ -40,8 +42,8 @@
   return ret;
 }
 
-struct FreeLHASH {
-  void operator()(_LHASH *lh) { lh_free(lh); }
+struct FreeLHASH_OF_char {
+  void operator()(LHASH_OF(char) *lh) { lh_char_free(lh); }
 };
 
 static const char *Lookup(
@@ -55,8 +57,8 @@
 }
 
 TEST(LHashTest, Basic) {
-  std::unique_ptr<_LHASH, FreeLHASH> lh(
-      lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
+  std::unique_ptr<LHASH_OF(char), FreeLHASH_OF_char> lh(
+      lh_char_new(lh_strhash, strcmp));
   ASSERT_TRUE(lh);
 
   // lh is expected to store a canonical instance of each string. dummy_lh
@@ -65,12 +67,12 @@
   std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
 
   for (unsigned i = 0; i < 100000; i++) {
-    EXPECT_EQ(dummy_lh.size(), lh_num_items(lh.get()));
+    EXPECT_EQ(dummy_lh.size(), lh_char_num_items(lh.get()));
 
-    // Check the entire contents and test |lh_doall_arg|. This takes O(N) time,
-    // so only do it every few iterations.
+    // Check the entire contents and test |lh_*_doall_arg|. This takes O(N)
+    // time, so only do it every few iterations.
     //
-    // TODO(davidben): |lh_doall_arg| also supports modifying the hash in the
+    // TODO(davidben): |lh_*_doall_arg| also supports modifying the hash in the
     // callback. Test this.
     if (i % 1000 == 0) {
       using ValueList = std::vector<const char *>;
@@ -80,14 +82,22 @@
       }
       std::sort(expected.begin(), expected.end());
 
-      lh_doall_arg(lh.get(),
-                   [](void *ptr, void *arg) {
-                     ValueList *out = reinterpret_cast<ValueList *>(arg);
-                     out->push_back(reinterpret_cast<char *>(ptr));
-                   },
-                   &actual);
+      lh_char_doall_arg(lh.get(),
+                        [](char *ptr, void *arg) {
+                          ValueList *out = reinterpret_cast<ValueList *>(arg);
+                          out->push_back(ptr);
+                        },
+                        &actual);
       std::sort(actual.begin(), actual.end());
+      EXPECT_EQ(expected, actual);
 
+      // Also test |lh_*_doall|.
+      actual.clear();
+      static ValueList *global_actual_list;
+      global_actual_list = &actual;
+      lh_char_doall(lh.get(),
+                    [](char *ptr) { global_actual_list->push_back(ptr); });
+      std::sort(actual.begin(), actual.end());
       EXPECT_EQ(expected, actual);
     }
 
@@ -101,17 +111,17 @@
     switch (action) {
       case kRetrieve: {
         std::unique_ptr<char[]> key = RandString();
-        void *value = lh_retrieve(lh.get(), key.get());
+        char *value = lh_char_retrieve(lh.get(), key.get());
         EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
 
-        // Do the same lookup with |lh_retrieve_key|.
-        value = lh_retrieve_key(
+        // Do the same lookup with |lh_char_retrieve_key|.
+        value = lh_char_retrieve_key(
             lh.get(), &key, lh_strhash(key.get()),
-            [](const void *key_ptr, const void *data) -> int {
+            [](const void *key_ptr, const char *data) -> int {
               const char *key_data =
                   reinterpret_cast<const std::unique_ptr<char[]> *>(key_ptr)
                       ->get();
-              return strcmp(key_data, reinterpret_cast<const char *>(data));
+              return strcmp(key_data, data);
             });
         EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
         break;
@@ -119,8 +129,8 @@
 
       case kInsert: {
         std::unique_ptr<char[]> key = RandString();
-        void *previous;
-        ASSERT_TRUE(lh_insert(lh.get(), &previous, key.get()));
+        char *previous;
+        ASSERT_TRUE(lh_char_insert(lh.get(), &previous, key.get()));
         EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
         dummy_lh[key.get()] = std::move(key);
         break;
@@ -128,7 +138,7 @@
 
       case kDelete: {
         std::unique_ptr<char[]> key = RandString();
-        void *value = lh_delete(lh.get(), key.get());
+        char *value = lh_char_delete(lh.get(), key.get());
         EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
         dummy_lh.erase(key.get());
         break;
diff --git a/crypto/lhash/make_macros.sh b/crypto/lhash/make_macros.sh
deleted file mode 100644
index 1418539..0000000
--- a/crypto/lhash/make_macros.sh
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/bin/sh
-
-include_dir=../../include/openssl
-out=${include_dir}/lhash_macros.h
-
-cat > $out << EOF
-/* Copyright (c) 2014, Google Inc.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
-
-#if !defined(IN_LHASH_H)
-#error "Don't include this file directly. Include lhash.h"
-#endif
-
-EOF
-
-output_lhash () {
-  type=$1
-
-  cat >> $out << EOF
-// ${type}
-#define lh_${type}_new(hash, comp)\\
-((LHASH_OF(${type})*) lh_new(CHECKED_CAST(lhash_hash_func, uint32_t (*) (const ${type} *), hash), CHECKED_CAST(lhash_cmp_func, int (*) (const ${type} *a, const ${type} *b), comp)))
-
-#define lh_${type}_free(lh)\\
-  lh_free(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh));
-
-#define lh_${type}_num_items(lh)\\
-  lh_num_items(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh))
-
-#define lh_${type}_retrieve(lh, data)\\
-  ((${type}*) lh_retrieve(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh), CHECKED_CAST(void*, ${type}*, data)))
-
-#define lh_${type}_retrieve_key(lh, key, key_hash, cmp_key)\\
-  ((${type}*) lh_retrieve_key(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh), key, key_hash, CHECKED_CAST(int (*)(const void *, const void *), int (*)(const void *, const ${type} *), cmp_key)))
-
-#define lh_${type}_insert(lh, old_data, data)\\
-  lh_insert(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh), CHECKED_CAST(void**, ${type}**, old_data), CHECKED_CAST(void*, ${type}*, data))
-
-#define lh_${type}_delete(lh, data)\\
-  ((${type}*) lh_delete(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh), CHECKED_CAST(void*, ${type}*, data)))
-
-#define lh_${type}_doall(lh, func)\\
-  lh_doall(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh), CHECKED_CAST(void (*)(void*), void (*) (${type}*), func));
-
-#define lh_${type}_doall_arg(lh, func, arg)\\
-  lh_doall_arg(CHECKED_CAST(_LHASH*, LHASH_OF(${type})*, lh), CHECKED_CAST(void (*)(void*, void*), void (*) (${type}*, void*), func), arg);
-
-
-EOF
-}
-
-lhash_types=$(cat ${include_dir}/lhash.h | grep '^// LHASH_OF:' | sed -e 's/.*LHASH_OF://' -e 's/ .*//')
-
-for type in $lhash_types; do
-  echo Hash of ${type}
-  output_lhash "${type}"
-done
-
-clang-format -i $out
diff --git a/crypto/obj/obj.c b/crypto/obj/obj.c
index c928889..235f7d6 100644
--- a/crypto/obj/obj.c
+++ b/crypto/obj/obj.c
@@ -76,6 +76,8 @@
 #include "../internal.h"
 
 
+DEFINE_LHASH_OF(ASN1_OBJECT)
+
 static struct CRYPTO_STATIC_MUTEX global_added_lock = CRYPTO_STATIC_MUTEX_INIT;
 // These globals are protected by |global_added_lock|.
 static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL;
diff --git a/crypto/pool/pool.c b/crypto/pool/pool.c
index 15c7484..c53210a 100644
--- a/crypto/pool/pool.c
+++ b/crypto/pool/pool.c
@@ -26,6 +26,8 @@
 #include "internal.h"
 
 
+DEFINE_LHASH_OF(CRYPTO_BUFFER)
+
 static uint32_t CRYPTO_BUFFER_hash(const CRYPTO_BUFFER *buf) {
   return OPENSSL_hash32(buf->data, buf->len);
 }
diff --git a/include/openssl/lhash.h b/include/openssl/lhash.h
index 287ad63..77ca493 100644
--- a/include/openssl/lhash.h
+++ b/include/openssl/lhash.h
@@ -87,23 +87,8 @@
 
 #define LHASH_OF(type) struct lhash_st_##type
 
-#define DEFINE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
-
 #define DECLARE_LHASH_OF(type) LHASH_OF(type);
 
-// The make_macros.sh script in this directory parses the following lines and
-// generates the lhash_macros.h file that contains macros for the following
-// types of stacks:
-//
-// LHASH_OF:ASN1_OBJECT
-// LHASH_OF:CONF_VALUE
-// LHASH_OF:CRYPTO_BUFFER
-// LHASH_OF:SSL_SESSION
-
-#define IN_LHASH_H
-#include <openssl/lhash_macros.h>
-#undef IN_LHASH_H
-
 
 // lhash_item_st is an element of a hash chain. It points to the opaque data
 // for this element and to the next item in the chain. The linked-list is NULL
@@ -175,6 +160,63 @@
 // strings.
 OPENSSL_EXPORT uint32_t lh_strhash(const char *c);
 
+#define DEFINE_LHASH_OF(type)                                                  \
+  DECLARE_LHASH_OF(type)                                                       \
+                                                                               \
+  typedef int (*lhash_##type##_cmp_func)(const type *, const type *);          \
+  typedef uint32_t (*lhash_##type##_hash_func)(const type *);                  \
+                                                                               \
+  OPENSSL_INLINE LHASH_OF(type) *                                              \
+      lh_##type##_new(lhash_##type##_hash_func hash,                           \
+                      lhash_##type##_cmp_func comp) {                          \
+    return (LHASH_OF(type) *)lh_new((lhash_hash_func)hash,                     \
+                                    (lhash_cmp_func)comp);                     \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE void lh_##type##_free(LHASH_OF(type) *lh) {                   \
+    lh_free((_LHASH *)lh);                                                     \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE size_t lh_##type##_num_items(const LHASH_OF(type) *lh) {      \
+    return lh_num_items((const _LHASH *)lh);                                   \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE type *lh_##type##_retrieve(const LHASH_OF(type) *lh,          \
+                                            const type *data) {                \
+    return (type *)lh_retrieve((const _LHASH *)lh, data);                      \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE type *lh_##type##_retrieve_key(                               \
+      const LHASH_OF(type) *lh, const void *key, uint32_t key_hash,            \
+      int (*cmp_key)(const void *key, const type *value)) {                    \
+    return (type *)lh_retrieve_key(                                            \
+        (const _LHASH *)lh, key, key_hash,                                     \
+        (int (*)(const void *, const void *))cmp_key);                         \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE int lh_##type##_insert(LHASH_OF(type) *lh, type **old_data,   \
+                                        type *data) {                          \
+    void *old_data_void = NULL;                                                \
+    int ret = lh_insert((_LHASH *)lh, &old_data_void, data);                   \
+    *old_data = (type *)old_data_void;                                         \
+    return ret;                                                                \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE type *lh_##type##_delete(LHASH_OF(type) *lh,                  \
+                                          const type *data) {                  \
+    return (type *)lh_delete((_LHASH *)lh, data);                              \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE void lh_##type##_doall(LHASH_OF(type) *lh,                    \
+                                        void (*func)(type *)) {                \
+    lh_doall((_LHASH *)lh, (void (*)(void *))func);                            \
+  }                                                                            \
+                                                                               \
+  OPENSSL_INLINE void lh_##type##_doall_arg(                                   \
+      LHASH_OF(type) *lh, void (*func)(type *, void *), void *arg) {           \
+    lh_doall_arg((_LHASH *)lh, (void (*)(void *, void *))func, arg);           \
+  }
+
 
 #if defined(__cplusplus)
 }  // extern C
diff --git a/include/openssl/lhash_macros.h b/include/openssl/lhash_macros.h
deleted file mode 100644
index dd3e4dc..0000000
--- a/include/openssl/lhash_macros.h
+++ /dev/null
@@ -1,198 +0,0 @@
-/* Copyright (c) 2014, Google Inc.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
-
-#if !defined(IN_LHASH_H)
-#error "Don't include this file directly. Include lhash.h"
-#endif
-
-// ASN1_OBJECT
-#define lh_ASN1_OBJECT_new(hash, comp)                                       \
-  ((LHASH_OF(ASN1_OBJECT) *)lh_new(                                          \
-      CHECKED_CAST(lhash_hash_func, uint32_t(*)(const ASN1_OBJECT *), hash), \
-      CHECKED_CAST(lhash_cmp_func,                                           \
-                   int (*)(const ASN1_OBJECT *a, const ASN1_OBJECT *b),      \
-                   comp)))
-
-#define lh_ASN1_OBJECT_free(lh) \
-  lh_free(CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh));
-
-#define lh_ASN1_OBJECT_num_items(lh) \
-  lh_num_items(CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh))
-
-#define lh_ASN1_OBJECT_retrieve(lh, data)                  \
-  ((ASN1_OBJECT *)lh_retrieve(                             \
-      CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh), \
-      CHECKED_CAST(void *, ASN1_OBJECT *, data)))
-
-#define lh_ASN1_OBJECT_retrieve_key(lh, key, key_hash, cmp_key)           \
-  ((ASN1_OBJECT *)lh_retrieve_key(                                        \
-      CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh), key, key_hash, \
-      CHECKED_CAST(int (*)(const void *, const void *),                   \
-                   int (*)(const void *, const ASN1_OBJECT *), cmp_key)))
-
-#define lh_ASN1_OBJECT_insert(lh, old_data, data)                \
-  lh_insert(CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh), \
-            CHECKED_CAST(void **, ASN1_OBJECT **, old_data),     \
-            CHECKED_CAST(void *, ASN1_OBJECT *, data))
-
-#define lh_ASN1_OBJECT_delete(lh, data)                    \
-  ((ASN1_OBJECT *)lh_delete(                               \
-      CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh), \
-      CHECKED_CAST(void *, ASN1_OBJECT *, data)))
-
-#define lh_ASN1_OBJECT_doall(lh, func)                          \
-  lh_doall(CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh), \
-           CHECKED_CAST(void (*)(void *), void (*)(ASN1_OBJECT *), func));
-
-#define lh_ASN1_OBJECT_doall_arg(lh, func, arg)                     \
-  lh_doall_arg(CHECKED_CAST(_LHASH *, LHASH_OF(ASN1_OBJECT) *, lh), \
-               CHECKED_CAST(void (*)(void *, void *),               \
-                            void (*)(ASN1_OBJECT *, void *), func), \
-               arg);
-
-
-// CONF_VALUE
-#define lh_CONF_VALUE_new(hash, comp)                                       \
-  ((LHASH_OF(CONF_VALUE) *)lh_new(                                          \
-      CHECKED_CAST(lhash_hash_func, uint32_t(*)(const CONF_VALUE *), hash), \
-      CHECKED_CAST(lhash_cmp_func,                                          \
-                   int (*)(const CONF_VALUE *a, const CONF_VALUE *b), comp)))
-
-#define lh_CONF_VALUE_free(lh) \
-  lh_free(CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh));
-
-#define lh_CONF_VALUE_num_items(lh) \
-  lh_num_items(CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh))
-
-#define lh_CONF_VALUE_retrieve(lh, data)                  \
-  ((CONF_VALUE *)lh_retrieve(                             \
-      CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh), \
-      CHECKED_CAST(void *, CONF_VALUE *, data)))
-
-#define lh_CONF_VALUE_retrieve_key(lh, key, key_hash, cmp_key)           \
-  ((CONF_VALUE *)lh_retrieve_key(                                        \
-      CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh), key, key_hash, \
-      CHECKED_CAST(int (*)(const void *, const void *),                  \
-                   int (*)(const void *, const CONF_VALUE *), cmp_key)))
-
-#define lh_CONF_VALUE_insert(lh, old_data, data)                \
-  lh_insert(CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh), \
-            CHECKED_CAST(void **, CONF_VALUE **, old_data),     \
-            CHECKED_CAST(void *, CONF_VALUE *, data))
-
-#define lh_CONF_VALUE_delete(lh, data)                                         \
-  ((CONF_VALUE *)lh_delete(CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh), \
-                           CHECKED_CAST(void *, CONF_VALUE *, data)))
-
-#define lh_CONF_VALUE_doall(lh, func)                          \
-  lh_doall(CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh), \
-           CHECKED_CAST(void (*)(void *), void (*)(CONF_VALUE *), func));
-
-#define lh_CONF_VALUE_doall_arg(lh, func, arg)                     \
-  lh_doall_arg(CHECKED_CAST(_LHASH *, LHASH_OF(CONF_VALUE) *, lh), \
-               CHECKED_CAST(void (*)(void *, void *),              \
-                            void (*)(CONF_VALUE *, void *), func), \
-               arg);
-
-
-// CRYPTO_BUFFER
-#define lh_CRYPTO_BUFFER_new(hash, comp)                                       \
-  ((LHASH_OF(CRYPTO_BUFFER) *)lh_new(                                          \
-      CHECKED_CAST(lhash_hash_func, uint32_t(*)(const CRYPTO_BUFFER *), hash), \
-      CHECKED_CAST(lhash_cmp_func,                                             \
-                   int (*)(const CRYPTO_BUFFER *a, const CRYPTO_BUFFER *b),    \
-                   comp)))
-
-#define lh_CRYPTO_BUFFER_free(lh) \
-  lh_free(CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh));
-
-#define lh_CRYPTO_BUFFER_num_items(lh) \
-  lh_num_items(CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh))
-
-#define lh_CRYPTO_BUFFER_retrieve(lh, data)                  \
-  ((CRYPTO_BUFFER *)lh_retrieve(                             \
-      CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh), \
-      CHECKED_CAST(void *, CRYPTO_BUFFER *, data)))
-
-#define lh_CRYPTO_BUFFER_retrieve_key(lh, key, key_hash, cmp_key)           \
-  ((CRYPTO_BUFFER *)lh_retrieve_key(                                        \
-      CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh), key, key_hash, \
-      CHECKED_CAST(int (*)(const void *, const void *),                     \
-                   int (*)(const void *, const CRYPTO_BUFFER *), cmp_key)))
-
-#define lh_CRYPTO_BUFFER_insert(lh, old_data, data)                \
-  lh_insert(CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh), \
-            CHECKED_CAST(void **, CRYPTO_BUFFER **, old_data),     \
-            CHECKED_CAST(void *, CRYPTO_BUFFER *, data))
-
-#define lh_CRYPTO_BUFFER_delete(lh, data)                    \
-  ((CRYPTO_BUFFER *)lh_delete(                               \
-      CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh), \
-      CHECKED_CAST(void *, CRYPTO_BUFFER *, data)))
-
-#define lh_CRYPTO_BUFFER_doall(lh, func)                          \
-  lh_doall(CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh), \
-           CHECKED_CAST(void (*)(void *), void (*)(CRYPTO_BUFFER *), func));
-
-#define lh_CRYPTO_BUFFER_doall_arg(lh, func, arg)                     \
-  lh_doall_arg(CHECKED_CAST(_LHASH *, LHASH_OF(CRYPTO_BUFFER) *, lh), \
-               CHECKED_CAST(void (*)(void *, void *),                 \
-                            void (*)(CRYPTO_BUFFER *, void *), func), \
-               arg);
-
-
-// SSL_SESSION
-#define lh_SSL_SESSION_new(hash, comp)                                       \
-  ((LHASH_OF(SSL_SESSION) *)lh_new(                                          \
-      CHECKED_CAST(lhash_hash_func, uint32_t(*)(const SSL_SESSION *), hash), \
-      CHECKED_CAST(lhash_cmp_func,                                           \
-                   int (*)(const SSL_SESSION *a, const SSL_SESSION *b),      \
-                   comp)))
-
-#define lh_SSL_SESSION_free(lh) \
-  lh_free(CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh));
-
-#define lh_SSL_SESSION_num_items(lh) \
-  lh_num_items(CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh))
-
-#define lh_SSL_SESSION_retrieve(lh, data)                  \
-  ((SSL_SESSION *)lh_retrieve(                             \
-      CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh), \
-      CHECKED_CAST(void *, SSL_SESSION *, data)))
-
-#define lh_SSL_SESSION_retrieve_key(lh, key, key_hash, cmp_key)           \
-  ((SSL_SESSION *)lh_retrieve_key(                                        \
-      CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh), key, key_hash, \
-      CHECKED_CAST(int (*)(const void *, const void *),                   \
-                   int (*)(const void *, const SSL_SESSION *), cmp_key)))
-
-#define lh_SSL_SESSION_insert(lh, old_data, data)                \
-  lh_insert(CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh), \
-            CHECKED_CAST(void **, SSL_SESSION **, old_data),     \
-            CHECKED_CAST(void *, SSL_SESSION *, data))
-
-#define lh_SSL_SESSION_delete(lh, data)                    \
-  ((SSL_SESSION *)lh_delete(                               \
-      CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh), \
-      CHECKED_CAST(void *, SSL_SESSION *, data)))
-
-#define lh_SSL_SESSION_doall(lh, func)                          \
-  lh_doall(CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh), \
-           CHECKED_CAST(void (*)(void *), void (*)(SSL_SESSION *), func));
-
-#define lh_SSL_SESSION_doall_arg(lh, func, arg)                     \
-  lh_doall_arg(CHECKED_CAST(_LHASH *, LHASH_OF(SSL_SESSION) *, lh), \
-               CHECKED_CAST(void (*)(void *, void *),               \
-                            void (*)(SSL_SESSION *, void *), func), \
-               arg);
diff --git a/include/openssl/type_check.h b/include/openssl/type_check.h
index e5d7047..b5d59b5 100644
--- a/include/openssl/type_check.h
+++ b/include/openssl/type_check.h
@@ -64,14 +64,6 @@
 #endif
 
 
-// This header file contains some common macros for enforcing type checking.
-// Several, common OpenSSL structures (i.e. stack and lhash) operate on void
-// pointers, but we wish to have type checking when they are used with a
-// specific type.
-
-// CHECKED_CAST casts |p| from type |from| to type |to|.
-#define CHECKED_CAST(to, from, p) ((to) (1 ? (p) : (from)0))
-
 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
 #define OPENSSL_COMPILE_ASSERT(cond, msg) _Static_assert(cond, #msg)
 #else
diff --git a/ssl/internal.h b/ssl/internal.h
index 561b5d9..2218992 100644
--- a/ssl/internal.h
+++ b/ssl/internal.h
@@ -2013,7 +2013,7 @@
 
 BSSL_NAMESPACE_END
 
-DECLARE_LHASH_OF(SSL_SESSION)
+DEFINE_LHASH_OF(SSL_SESSION)
 
 DEFINE_NAMED_STACK_OF(CertCompressionAlg, bssl::CertCompressionAlg);