Glib is a fantastic lib

This commit is contained in:
Kolan Sh 2012-01-20 19:18:19 +04:00
parent 325f7a739c
commit accb7dcc8f
4 changed files with 100 additions and 33 deletions

View File

@ -18,3 +18,10 @@ cscope
.ctags
.cscope
syntax: regexp
#[^.]+[.][^.]+$
#[!]re:[^.]+[.][^.]+$
#^(?!(scripts|foo|bar)/)[^/]+/
#^[^.]*$
#^(\.c$)
#[.]+/[^\.]+$

View File

@ -4,23 +4,26 @@
# No need to call make clean if You make with other mode,
# because the Makefile containes rules for automatically clean.
# Some usage examples:
# CFLAGS="-march=core2 -mtune=core2" make
# LDFLAGS="-lexpat" make mode=profile
# make # default mode is debug
# CFLAGS="-O2 -march=core2 -mtune=core2 --msse4.1 mfpmath=sse -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1 -Wl,-ass-needed" make mode=develop
# CFLAGS="-O2 -march=amdfam10 -mtune=amdfam10 -msse4a --mfpmath=sse -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1 -Wl,--as-needed" make mode=profile
# CFLAGS="-O2 -march=k6-2 -mtune=k6-2 -m3dnow --mfpmath=387 -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1 -Wl,--as-needed" make mode=release
# Report bugs to <mecareful@gmail.com>
#_________________________________
# ENVIRONMENT |
#________________________________|
CC=cc
CXX=c++
LIBRARIES=-lstdc++
TARGET0=realloc_speed_test_c
TARGET1=realloc_speed_test_cpp
TARGET= $(TARGET0) $(TARGET1)
INCLUDE1=$(HOME)/projects/include
INCLUDE2=/usr/local/include
INCLUDE=-I$(INCLUDE1) -I$(INCLUDE2)
TARGET2=realloc_speed_test_glib
TARGETS= $(TARGET0) $(TARGET1) $(TARGET2)
CC=cc
CXX=c++
LIBS=-lglib-2.0 -lstdc++
SRC1=$(HOME)/projects/texreport/mod-alloc
SRC2=$(HOME)/projects/texreport/mod-err
SRC=-I$(SRC1) -I$(SRC2)
INCLUDES=-I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include
#________________________________________
# BUILD SCRIPT (don't change) |
#_______________________________________|
@ -28,26 +31,26 @@ ifeq ($(mode),)
mode = debug
endif
ifeq ($(mode),debug)
CFLAGS = -O0 -g -std=gnu99 -pedantic -Wextra -Wconversion
LDFLAGS =
CFLAGS := -O0 -g -std=gnu99 -pedantic -Wextra -Wconversion $(CFLAGS)
LDFLAGS := $(LDFLAGS)
endif
ifeq ($(mode),profile)
CFLAGS = -O0 -g -p -ftest-coverage -Wcoverage-mismatch
LDFLAGS = -g -p
CFLAGS := -O0 -g -p -ftest-coverage -Wcoverage-mismatch $(CFLAGS)
LDFLAGS := -g -p $(LDFLAGS)
endif
ifeq ($(mode),develop)
CFLAGS += -O2 -g
LDFLAGS += -O1
CFLAGS := -O2 -g $(CFLAGS)
LDFLAGS := -O1 $(LDFLAGS)
endif
ifeq ($(mode),release)
CFLAGS += -O2
LDFLAGS += -O1
CFLAGS := -O2 $(CFLAGS)
LDFLAGS := -O1 $(LDFLAGS)
endif
CFLAGS += -Wall
LDFLAGS += -Wall $(LIBRARIES)
CFLAGS += -Wall $(INCLUDES)
LDFLAGS += -Wall $(LIBS)
all: change_make_options $(TARGET)
all: change_make_options $(TARGETS)
ifneq ($(mode),debug)
ifneq ($(mode),profile)
@ -73,16 +76,16 @@ change_make_options:
ifneq ($(mode)|$(CFLAGS)|$(LDFLAGS), $(OLD_BUILD_MODE)|$(OLD_BUILD_CFLAGS)|$(OLD_BUILD_LDFLAGS))
@echo CLEANING...
@make clean &>/dev/null
@echo MODE=$(mode) > make_options.out
@echo CFLAGS=$(CFLAGS) >> make_options.out
@echo LDFLAGS=$(LDFLAGS) >> make_options.out
@echo "MODE=$(mode)" > make_options.out
@echo "CFLAGS=$(CFLAGS)" >> make_options.out
@echo "LDFLAGS=$(LDFLAGS)" >> make_options.out
endif
%.o :
$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
$(CC) -c $(CFLAGS) $(SRC) -o $@ $<
clean:
$(RM) *.o *.out callgrind.out.* *.gcno $(TARGET)
$(RM) *.o *.out callgrind.out.* *.gcno $(TARGETS)
.PHONY: all change_make_options clean
@ -105,19 +108,32 @@ $(TARGET1): $(target_objs1)
$(CC) $(LDFLAGS) -o $@ $(target_objs1)
target_objs2 = \
realloc_speed_test_glib.o \
xalloc.o \
xerror.o
$(TARGET2): $(target_objs2)
$(CC) $(LDFLAGS) -o $@ $(target_objs2)
realloc_speed_test_c.o: \
realloc_speed_test_c.c \
$(INCLUDE1)/xalloc.h
$(SRC1)/xalloc.h
realloc_speed_test_cpp.o: \
realloc_speed_test_cpp.cpp
realloc_speed_test_glib.o: \
realloc_speed_test_glib.c \
$(SRC1)/xalloc.h
xalloc.o: \
$(INCLUDE1)/xalloc.c \
$(INCLUDE1)/xalloc.h \
$(INCLUDE1)/xerror.h
$(SRC1)/xalloc.c \
$(SRC1)/xalloc.h \
$(SRC2)/xerror.h
xerror.o: \
$(INCLUDE1)/xerror.c \
$(INCLUDE1)/xerror.h
$(SRC2)/xerror.c \
$(SRC2)/xerror.h

View File

@ -14,7 +14,7 @@
int main(int argc, char *argv[])
{
if (argc != 3) {
fputs("Usage: realloc_speed_test_cpp iterations nobj\n", stderr);
fputs("Usage: realloc_speed_test_c iterations nobj\n", stderr);
exit(-1);
}

View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "xalloc.h"
int main(int argc, char *argv[])
{
if (argc != 3) {
fputs("Usage: realloc_speed_test_glib iterations nobj\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
GString **p = malloc(sizeof(GString *) * nobj);
memset(p, 0, sizeof(void *) * nobj);
size_t i, idx, sz;
for (i = 0; i < nobj; i++)
p[i] = g_string_new(0);
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;
p[idx] = g_string_append_c(p[idx], 'c');//realloc(p[idx], sz);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
for (i = 0; i < nobj; i++)
g_string_free(p[i], FALSE);
free(p);
printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);
return EXIT_SUCCESS;
}