Merge in changes to allow filtering received packets
Merge branch 'output-filter' Conflicts: src/zmap.c
This commit is contained in:
40
lib/stack.c
Normal file
40
lib/stack.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include "stack.h"
|
||||
#include "../lib/xalloc.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
struct stack {
|
||||
size_t max_size;
|
||||
size_t cur_size;
|
||||
void** arr;
|
||||
};
|
||||
|
||||
stack_t* alloc_stack(size_t size)
|
||||
{
|
||||
stack_t* stack = xmalloc(sizeof(stack_t));
|
||||
stack->arr = xcalloc(size, sizeof(void*));
|
||||
stack->max_size = size;
|
||||
stack->cur_size = 0;
|
||||
return stack;
|
||||
}
|
||||
|
||||
void free_stack(stack_t* stack)
|
||||
{
|
||||
xfree(stack->arr);
|
||||
xfree(stack);
|
||||
}
|
||||
|
||||
void push(stack_t* stack, void* elt)
|
||||
{
|
||||
if (stack->cur_size == stack->max_size) {
|
||||
stack->max_size *= 2;
|
||||
xrealloc(stack->arr, stack->max_size);
|
||||
}
|
||||
stack->arr[stack->cur_size++] = elt;
|
||||
}
|
||||
|
||||
void* pop(stack_t* stack)
|
||||
{
|
||||
void* res = stack->arr[--stack->cur_size];
|
||||
return res;
|
||||
}
|
15
lib/stack.h
Normal file
15
lib/stack.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef ZMAP_STACK_H
|
||||
#define ZMAP_STACK_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct stack;
|
||||
typedef struct stack stack_t;
|
||||
|
||||
stack_t* alloc_stack(size_t size);
|
||||
void free_stack(stack_t* stack);
|
||||
|
||||
void push(stack_t* stack, void* elt);
|
||||
void* pop(stack_t* stack);
|
||||
|
||||
#endif /* ZMAP_STACK_H */
|
44
lib/xalloc.c
Normal file
44
lib/xalloc.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include "xalloc.h"
|
||||
#include "../lib/logger.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void die() __attribute__((noreturn));
|
||||
|
||||
void* xcalloc(size_t count, size_t size)
|
||||
{
|
||||
void* res = calloc(count, size);
|
||||
if (res == NULL) {
|
||||
die();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void xfree(void *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void* xmalloc(size_t size)
|
||||
{
|
||||
void* res = malloc(size);
|
||||
if (res == NULL) {
|
||||
die();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void* xrealloc(void *ptr, size_t size)
|
||||
{
|
||||
void* res = realloc(ptr, size);
|
||||
if (res == NULL) {
|
||||
die();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void die()
|
||||
{
|
||||
log_fatal("zmap", "Out of memory");
|
||||
}
|
||||
|
14
lib/xalloc.h
Normal file
14
lib/xalloc.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef ZMAP_ALLOC_H
|
||||
#define ZMAP_ALLOC_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void* xcalloc(size_t count, size_t size);
|
||||
|
||||
void xfree(void *ptr);
|
||||
|
||||
void* xmalloc(size_t size);
|
||||
|
||||
void* xrealloc(void *ptr, size_t size);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user