File: unicap_cache.c

package info (click to toggle)
unicap 0.9.12-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 3,636 kB
  • ctags: 3,520
  • sloc: ansic: 23,011; sh: 10,712; xml: 1,408; makefile: 298
file content (118 lines) | stat: -rw-r--r-- 1,792 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
** unicap_cache.c
** 
** Made by (Arne Caspari)
** Login   <arne@localhost>
** 
** Started on  Fri Aug 31 19:07:15 2007 Arne Caspari
*/

#include "config.h"
#include <string.h>

#define NUM_CACHE_ENTRIES 64

typedef struct
{
      char key[64];
      void *value;
      int refc;
} unicap_cache_entry_t;

static unicap_cache_entry_t g_cache[ NUM_CACHE_ENTRIES ];
static int g_use_cache = 0;


void unicap_cache_init( void )
{
   int i;
   
   if( !g_use_cache )
   {
      for( i = 0; i < NUM_CACHE_ENTRIES; i++ )
      {
	 g_cache[ i ].value = NULL;
	 g_cache[ i ].refc = 0;
      }
   }

   g_use_cache = 1;
}


int unicap_cache_add( char *key, void *value )
{
   int i;
   int ret = -1;
   
   if( g_use_cache )
   {
      for( i = 0; i < NUM_CACHE_ENTRIES; i++ )
      {
	 if( !strcpy( g_cache[ i ].key, key ) )
	 {
	    ret = 1;
	 }
      }
      
      if( ret < 0 )
      {
	 for( i = 0; i < NUM_CACHE_ENTRIES; i++ )
	 {
	    if( !g_cache[ i ].value )
	    {
	       ret = 0;
	       strcpy( g_cache[ i ].key, key );
	       g_cache[ i ].refc++;
	       g_cache[ i ].value = value;
	    }
	 }
      }
   }
   
   return ret;
}

void* unicap_cache_get( char *key )
{
   int i;
   
   if( g_use_cache )
   {
      for( i = 0; i < NUM_CACHE_ENTRIES; i++ )
      {
	 if( !strcmp( g_cache[ i ].key, key ) )
	 {
	    g_cache[ i ].refc++;
	    return g_cache[ i ].value;
	 }
      }
   }
   
   return NULL;
}

void *unicap_cache_unref( char *key )
{
   int i;
   void *ret = NULL;
   
   if( g_use_cache )
   {
      for( i = 0; i < NUM_CACHE_ENTRIES; i++ )
      {
	 if( !strcmp( g_cache[ i ].key, key ) )
	 {
	    g_cache[ i ].refc--;
	    
	    if( !g_cache[ i ].refc )
	    {
	       ret = g_cache[ i ].value;
	    }
	    g_cache[ i ].value = NULL;
	 }
      }
   }

   return ret;
}