Home | History | Annotate | Download | only in squashfs-tools
      1 #ifndef CACHES_QUEUES_LISTS_H
      2 #define CACHES_QUEUES_LISTS_H
      3 /*
      4  * Create a squashfs filesystem.  This is a highly compressed read only
      5  * filesystem.
      6  *
      7  * Copyright (c) 2013, 2014
      8  * Phillip Lougher <phillip (at) squashfs.org.uk>
      9  *
     10  * This program is free software; you can redistribute it and/or
     11  * modify it under the terms of the GNU General Public License
     12  * as published by the Free Software Foundation; either version 2,
     13  * or (at your option) any later version.
     14  *
     15  * This program is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  * GNU General Public License for more details.
     19  *
     20  * You should have received a copy of the GNU General Public License
     21  * along with this program; if not, write to the Free Software
     22  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     23  *
     24  * caches-queues-lists.h
     25  */
     26 
     27 #define INSERT_LIST(NAME, TYPE) \
     28 void insert_##NAME##_list(TYPE **list, TYPE *entry) { \
     29 	if(*list) { \
     30 		entry->NAME##_next = *list; \
     31 		entry->NAME##_prev = (*list)->NAME##_prev; \
     32 		(*list)->NAME##_prev->NAME##_next = entry; \
     33 		(*list)->NAME##_prev = entry; \
     34 	} else { \
     35 		*list = entry; \
     36 		entry->NAME##_prev = entry->NAME##_next = entry; \
     37 	} \
     38 }
     39 
     40 
     41 #define REMOVE_LIST(NAME, TYPE) \
     42 void remove_##NAME##_list(TYPE **list, TYPE *entry) { \
     43 	if(entry->NAME##_prev == entry && entry->NAME##_next == entry) { \
     44 		/* only this entry in the list */ \
     45 		*list = NULL; \
     46 	} else if(entry->NAME##_prev != NULL && entry->NAME##_next != NULL) { \
     47 		/* more than one entry in the list */ \
     48 		entry->NAME##_next->NAME##_prev = entry->NAME##_prev; \
     49 		entry->NAME##_prev->NAME##_next = entry->NAME##_next; \
     50 		if(*list == entry) \
     51 			*list = entry->NAME##_next; \
     52 	} \
     53 	entry->NAME##_prev = entry->NAME##_next = NULL; \
     54 }
     55 
     56 
     57 #define INSERT_HASH_TABLE(NAME, TYPE, HASH_FUNCTION, FIELD, LINK) \
     58 void insert_##NAME##_hash_table(TYPE *container, struct file_buffer *entry) \
     59 { \
     60 	int hash = HASH_FUNCTION(entry->FIELD); \
     61 \
     62 	entry->LINK##_next = container->hash_table[hash]; \
     63 	container->hash_table[hash] = entry; \
     64 	entry->LINK##_prev = NULL; \
     65 	if(entry->LINK##_next) \
     66 		entry->LINK##_next->LINK##_prev = entry; \
     67 }
     68 
     69 
     70 #define REMOVE_HASH_TABLE(NAME, TYPE, HASH_FUNCTION, FIELD, LINK) \
     71 void remove_##NAME##_hash_table(TYPE *container, struct file_buffer *entry) \
     72 { \
     73 	if(entry->LINK##_prev) \
     74 		entry->LINK##_prev->LINK##_next = entry->LINK##_next; \
     75 	else \
     76 		container->hash_table[HASH_FUNCTION(entry->FIELD)] = \
     77 			entry->LINK##_next; \
     78 	if(entry->LINK##_next) \
     79 		entry->LINK##_next->LINK##_prev = entry->LINK##_prev; \
     80 \
     81 	entry->LINK##_prev = entry->LINK##_next = NULL; \
     82 }
     83 
     84 #define HASH_SIZE 65536
     85 #define CALCULATE_HASH(n) ((n) & 0xffff)
     86 
     87 
     88 /* struct describing a cache entry passed between threads */
     89 struct file_buffer {
     90 	union {
     91 		long long index;
     92 		long long sequence;
     93 	};
     94 	long long file_size;
     95 	union {
     96 		long long block;
     97 		unsigned short checksum;
     98 	};
     99 	struct cache *cache;
    100 	union {
    101 		struct file_info *dupl_start;
    102 		struct file_buffer *hash_next;
    103 	};
    104 	union {
    105 		int duplicate;
    106 		struct file_buffer *hash_prev;
    107 	};
    108 	union {
    109 		struct {
    110 			struct file_buffer *free_next;
    111 			struct file_buffer *free_prev;
    112 		};
    113 		struct {
    114 			struct file_buffer *seq_next;
    115 			struct file_buffer *seq_prev;
    116 		};
    117 	};
    118 	int size;
    119 	int c_byte;
    120 	char used;
    121 	char fragment;
    122 	char error;
    123 	char locked;
    124 	char wait_on_unlock;
    125 	char noD;
    126 	char data[0];
    127 };
    128 
    129 
    130 /* struct describing queues used to pass data between threads */
    131 struct queue {
    132 	int			size;
    133 	int			readp;
    134 	int			writep;
    135 	pthread_mutex_t		mutex;
    136 	pthread_cond_t		empty;
    137 	pthread_cond_t		full;
    138 	void			**data;
    139 };
    140 
    141 
    142 /*
    143  * struct describing seq_queues used to pass data between the read
    144  * thread and the deflate and main threads
    145  */
    146 struct seq_queue {
    147 	int			fragment_count;
    148 	int			block_count;
    149 	struct file_buffer	*hash_table[HASH_SIZE];
    150 	pthread_mutex_t		mutex;
    151 	pthread_cond_t		wait;
    152 };
    153 
    154 
    155 /* Cache status struct.  Caches are used to keep
    156   track of memory buffers passed between different threads */
    157 struct cache {
    158 	int	max_buffers;
    159 	int	count;
    160 	int	buffer_size;
    161 	int	noshrink_lookup;
    162 	int	first_freelist;
    163 	union {
    164 		int	used;
    165 		int	max_count;
    166 	};
    167 	pthread_mutex_t	mutex;
    168 	pthread_cond_t wait_for_free;
    169 	pthread_cond_t wait_for_unlock;
    170 	struct file_buffer *free_list;
    171 	struct file_buffer *hash_table[HASH_SIZE];
    172 };
    173 
    174 
    175 extern struct queue *queue_init(int);
    176 extern void queue_put(struct queue *, void *);
    177 extern void *queue_get(struct queue *);
    178 extern int queue_empty(struct queue *);
    179 extern void queue_flush(struct queue *);
    180 extern void dump_queue(struct queue *);
    181 extern struct seq_queue *seq_queue_init();
    182 extern void seq_queue_put(struct seq_queue *, struct file_buffer *);
    183 extern void dump_seq_queue(struct seq_queue *, int);
    184 extern struct file_buffer *seq_queue_get(struct seq_queue *);
    185 extern void seq_queue_flush(struct seq_queue *);
    186 extern struct cache *cache_init(int, int, int, int);
    187 extern struct file_buffer *cache_lookup(struct cache *, long long);
    188 extern struct file_buffer *cache_get(struct cache *, long long);
    189 extern struct file_buffer *cache_get_nohash(struct cache *);
    190 extern void cache_hash(struct file_buffer *, long long);
    191 extern void cache_block_put(struct file_buffer *);
    192 extern void dump_cache(struct cache *);
    193 extern struct file_buffer *cache_get_nowait(struct cache *, long long);
    194 extern struct file_buffer *cache_lookup_nowait(struct cache *, long long,
    195 	char *);
    196 extern void cache_wait_unlock(struct file_buffer *);
    197 extern void cache_unlock(struct file_buffer *);
    198 
    199 extern int first_freelist;
    200 #endif
    201