Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed the way malloc is handled in options.c #1535

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}

bin_PROGRAMS = ag
ag_SOURCES = src/ignore.c src/ignore.h src/log.c src/log.h src/options.c src/options.h src/print.c src/print_w32.c src/print.h src/scandir.c src/scandir.h src/search.c src/search.h src/lang.c src/lang.h src/util.c src/util.h src/decompress.c src/decompress.h src/uthash.h src/main.c src/zfile.c
ag_SOURCES = src/alloclist.c src/ignore.c src/ignore.h src/log.c src/log.h src/options.c src/options.h src/print.c src/print_w32.c src/print.h src/scandir.c src/scandir.h src/search.c src/search.h src/lang.c src/lang.h src/util.c src/util.h src/decompress.c src/decompress.h src/uthash.h src/main.c src/zfile.c
ag_LDADD = ${PCRE_LIBS} ${LZMA_LIBS} ${ZLIB_LIBS} $(PTHREAD_LIBS)

dist_man_MANS = doc/ag.1
Expand Down
1 change: 1 addition & 0 deletions Makefile.w32
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ SRCS = \
src/scandir.c \
src/search.c \
src/util.c \
src/alloclist.c \
src/print_w32.c
OBJS = $(subst .c,.o,$(SRCS))

Expand Down
47 changes: 47 additions & 0 deletions src/alloclist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdlib.h>
#include <stdio.h>
#include "alloclist.h"

// initialize allocation list
void alloc_list_init(alloc_list *list){
list->head = NULL;
}

// try to add a new allocation node to list
int alloc_list_add(alloc_list *list, void *data){
alloc_node *new_node = NULL;
if((new_node=(alloc_node*)malloc(sizeof(alloc_node)))==NULL){
return -1;
}
new_node->data = data;
new_node->next = list->head;
list->head = new_node;
return 0;
}

// remove the first element from allocation list
int alloc_list_pop(alloc_list *list, void **data){
if(list->head==NULL){
return -1;
}
alloc_node *old_node = list->head;
list->head = list->head->next;
old_node->next = NULL;
*data = old_node->data;
free(old_node);
return 0;
}

// destroy only the allocation list nodes without freeing what they point to
void alloc_list_destroy_clean(alloc_list *list){
void *data = NULL;
while(alloc_list_pop(list,&data)==0){}
}

// destroy the whole allocation list and free all elements it points to
void alloc_list_destroy(alloc_list *list){
void *data = NULL;
while(alloc_list_pop(list,&data)==0){
free(data);
}
}
22 changes: 22 additions & 0 deletions src/alloclist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ALLOC_LIST_H
#define ALLOC_LIST_H

struct alloc_node{
void *data;
struct alloc_node *next;
};

struct alloc_list{
struct alloc_node *head;
};

typedef struct alloc_node alloc_node;
typedef struct alloc_list alloc_list;

void alloc_list_init(alloc_list *list);
int alloc_list_add(alloc_list *list, void *data);
int alloc_list_pop(alloc_list *list, void **data);
void alloc_list_destroy_clean(alloc_list *list);
void alloc_list_destroy(alloc_list *list);

#endif
76 changes: 69 additions & 7 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "options.h"
#include "print.h"
#include "util.h"
#include "alloclist.h"

const char *color_line_number = "\033[1;33m"; /* bold yellow */
const char *color_match = "\033[30;43m"; /* black with yellow background */
Expand Down Expand Up @@ -234,8 +235,10 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
size_t *ext_index = NULL;
char *extensions = NULL;
size_t num_exts = 0;
alloc_list alist;

init_options();
alloc_list_init(&alist);

option_t base_longopts[] = {
{ "ackmate", no_argument, &opts.ackmate, 1 },
Expand Down Expand Up @@ -337,9 +340,29 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
lang_count = get_lang_count();
longopts_len = (sizeof(base_longopts) / sizeof(option_t));
full_len = (longopts_len + lang_count + 1);
longopts = ag_malloc(full_len * sizeof(option_t));

longopts = (option_t*)malloc(full_len * sizeof(option_t));
if(longopts==NULL){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}
if(alloc_list_add(&alist,longopts)!=0){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}

memcpy(longopts, base_longopts, sizeof(base_longopts));
ext_index = (size_t *)ag_malloc(sizeof(size_t) * lang_count);

ext_index = (size_t*)malloc(sizeof(size_t) * lang_count);
if(ext_index==NULL){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}
if(alloc_list_add(&alist,ext_index)!=0){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}

memset(ext_index, 0, sizeof(size_t) * lang_count);

for (i = 0; i < lang_count; i++) {
Expand Down Expand Up @@ -818,7 +841,17 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
}
(*paths)[i] = path;
#ifdef PATH_MAX
tmp = ag_malloc(PATH_MAX);

tmp = malloc(PATH_MAX);
if(tmp==NULL){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}
if(alloc_list_add(&alist,tmp)!=0){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}

base_path = realpath(path, tmp);
#else
base_path = realpath(path, NULL);
Expand All @@ -838,11 +871,39 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
opts.search_stream = 0;
} else {
path = ag_strdup(".");
*paths = ag_malloc(sizeof(char *) * 2);
*base_paths = ag_malloc(sizeof(char *) * 2);

*paths = malloc(sizeof(char *) * 2);
if(*paths==NULL){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}
if(alloc_list_add(&alist,*paths)!=0){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}

*base_paths = malloc(sizeof(char *) * 2);
if(*base_paths==NULL){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}
if(alloc_list_add(&alist,*base_paths)!=0){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}

(*paths)[0] = path;
#ifdef PATH_MAX
tmp = ag_malloc(PATH_MAX);
tmp = malloc(PATH_MAX);
if(tmp==NULL){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}
if(alloc_list_add(&alist,tmp)!=0){
alloc_list_destroy(&alist);
die("Memory allocation failed.");
}

(*base_paths)[0] = realpath(path, tmp);
#else
(*base_paths)[0] = realpath(path, NULL);
Expand All @@ -855,4 +916,5 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
#ifdef _WIN32
windows_use_ansi(opts.color_win_ansi);
#endif
}
alloc_list_destroy_clean(&alist);
}