diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/modules/test/Makefile b/modules/test/Makefile index 29cc50b..37f67b2 100644 --- a/modules/test/Makefile +++ b/modules/test/Makefile @@ -10,7 +10,7 @@ # TOPDIR ?= ../.. RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe +NAME = ut TARGET = $(NAME) SUBDIRS = USE_SO_VERSION = diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/modules/test/Makefile b/modules/test/Makefile index 29cc50b..37f67b2 100644 --- a/modules/test/Makefile +++ b/modules/test/Makefile @@ -10,7 +10,7 @@ # TOPDIR ?= ../.. RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe +NAME = ut TARGET = $(NAME) SUBDIRS = USE_SO_VERSION = diff --git a/modules/test/src/test_env.c b/modules/test/src/test_env.c index 0321563..a88d6e1 100644 --- a/modules/test/src/test_env.c +++ b/modules/test/src/test_env.c @@ -24,7 +24,6 @@ { // 事前設定 KcEnv_remove("UT_TEST_ENV"); - // 値設定 bool ret = KcEnv_set("UT_TEST_ENV", "ABC", false); assert_true(ret); @@ -33,11 +32,12 @@ ret = KcEnv_set("UT_TEST_ENV", "XYZ", false); assert_true(ret); char *env_val = KcEnv_get("UT_TEST_ENV"); - assert_equals("ABC", env_val); + assert_equals((const char *)"ABC", env_val); // 上書きあり、既に存在する。 ret = KcEnv_set("UT_TEST_ENV", "XYZ", true); assert_true(ret); + printf("--6--\n"); env_val = KcEnv_get("UT_TEST_ENV"); assert_equals("XYZ", env_val); diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/modules/test/Makefile b/modules/test/Makefile index 29cc50b..37f67b2 100644 --- a/modules/test/Makefile +++ b/modules/test/Makefile @@ -10,7 +10,7 @@ # TOPDIR ?= ../.. RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe +NAME = ut TARGET = $(NAME) SUBDIRS = USE_SO_VERSION = diff --git a/modules/test/src/test_env.c b/modules/test/src/test_env.c index 0321563..a88d6e1 100644 --- a/modules/test/src/test_env.c +++ b/modules/test/src/test_env.c @@ -24,7 +24,6 @@ { // 事前設定 KcEnv_remove("UT_TEST_ENV"); - // 値設定 bool ret = KcEnv_set("UT_TEST_ENV", "ABC", false); assert_true(ret); @@ -33,11 +32,12 @@ ret = KcEnv_set("UT_TEST_ENV", "XYZ", false); assert_true(ret); char *env_val = KcEnv_get("UT_TEST_ENV"); - assert_equals("ABC", env_val); + assert_equals((const char *)"ABC", env_val); // 上書きあり、既に存在する。 ret = KcEnv_set("UT_TEST_ENV", "XYZ", true); assert_true(ret); + printf("--6--\n"); env_val = KcEnv_get("UT_TEST_ENV"); assert_equals("XYZ", env_val); diff --git a/modules/test/src/test_lock_guard.c b/modules/test/src/test_lock_guard.c index 9c3249f..378646d 100644 --- a/modules/test/src/test_lock_guard.c +++ b/modules/test/src/test_lock_guard.c @@ -66,11 +66,14 @@ */ static void test_lock_guard_init_invalid_mutex(void) { +#if !(KC_IS_WINDOWS) + // Windows の場合は、エラー応答でなく落ちる。 mtx_t mutex; mtx_init(&mutex, mtx_plain | mtx_recursive); mtx_destroy(&mutex); KcLockGuard lock_guard = kc_lock_guard_init(&mutex, __FILE__, __func__, __LINE__); assert_null(lock_guard.mutex); +#endif } /** diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/modules/test/Makefile b/modules/test/Makefile index 29cc50b..37f67b2 100644 --- a/modules/test/Makefile +++ b/modules/test/Makefile @@ -10,7 +10,7 @@ # TOPDIR ?= ../.. RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe +NAME = ut TARGET = $(NAME) SUBDIRS = USE_SO_VERSION = diff --git a/modules/test/src/test_env.c b/modules/test/src/test_env.c index 0321563..a88d6e1 100644 --- a/modules/test/src/test_env.c +++ b/modules/test/src/test_env.c @@ -24,7 +24,6 @@ { // 事前設定 KcEnv_remove("UT_TEST_ENV"); - // 値設定 bool ret = KcEnv_set("UT_TEST_ENV", "ABC", false); assert_true(ret); @@ -33,11 +32,12 @@ ret = KcEnv_set("UT_TEST_ENV", "XYZ", false); assert_true(ret); char *env_val = KcEnv_get("UT_TEST_ENV"); - assert_equals("ABC", env_val); + assert_equals((const char *)"ABC", env_val); // 上書きあり、既に存在する。 ret = KcEnv_set("UT_TEST_ENV", "XYZ", true); assert_true(ret); + printf("--6--\n"); env_val = KcEnv_get("UT_TEST_ENV"); assert_equals("XYZ", env_val); diff --git a/modules/test/src/test_lock_guard.c b/modules/test/src/test_lock_guard.c index 9c3249f..378646d 100644 --- a/modules/test/src/test_lock_guard.c +++ b/modules/test/src/test_lock_guard.c @@ -66,11 +66,14 @@ */ static void test_lock_guard_init_invalid_mutex(void) { +#if !(KC_IS_WINDOWS) + // Windows の場合は、エラー応答でなく落ちる。 mtx_t mutex; mtx_init(&mutex, mtx_plain | mtx_recursive); mtx_destroy(&mutex); KcLockGuard lock_guard = kc_lock_guard_init(&mutex, __FILE__, __func__, __LINE__); assert_null(lock_guard.mutex); +#endif } /** diff --git a/modules/test/src/test_memory.c b/modules/test/src/test_memory.c index b8b953e..a775651 100644 --- a/modules/test/src/test_memory.c +++ b/modules/test/src/test_memory.c @@ -303,7 +303,11 @@ ptr = raw_aligned_alloc(sizeof(int), sizeof(int)); assert_not_null(ptr); +#if (KC_IS_WINDOWS) + _aligned_free(ptr); +#else raw_free(ptr); +#endif ptr = raw_calloc(10, sizeof(int)); assert_not_null(ptr); diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/modules/test/Makefile b/modules/test/Makefile index 29cc50b..37f67b2 100644 --- a/modules/test/Makefile +++ b/modules/test/Makefile @@ -10,7 +10,7 @@ # TOPDIR ?= ../.. RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe +NAME = ut TARGET = $(NAME) SUBDIRS = USE_SO_VERSION = diff --git a/modules/test/src/test_env.c b/modules/test/src/test_env.c index 0321563..a88d6e1 100644 --- a/modules/test/src/test_env.c +++ b/modules/test/src/test_env.c @@ -24,7 +24,6 @@ { // 事前設定 KcEnv_remove("UT_TEST_ENV"); - // 値設定 bool ret = KcEnv_set("UT_TEST_ENV", "ABC", false); assert_true(ret); @@ -33,11 +32,12 @@ ret = KcEnv_set("UT_TEST_ENV", "XYZ", false); assert_true(ret); char *env_val = KcEnv_get("UT_TEST_ENV"); - assert_equals("ABC", env_val); + assert_equals((const char *)"ABC", env_val); // 上書きあり、既に存在する。 ret = KcEnv_set("UT_TEST_ENV", "XYZ", true); assert_true(ret); + printf("--6--\n"); env_val = KcEnv_get("UT_TEST_ENV"); assert_equals("XYZ", env_val); diff --git a/modules/test/src/test_lock_guard.c b/modules/test/src/test_lock_guard.c index 9c3249f..378646d 100644 --- a/modules/test/src/test_lock_guard.c +++ b/modules/test/src/test_lock_guard.c @@ -66,11 +66,14 @@ */ static void test_lock_guard_init_invalid_mutex(void) { +#if !(KC_IS_WINDOWS) + // Windows の場合は、エラー応答でなく落ちる。 mtx_t mutex; mtx_init(&mutex, mtx_plain | mtx_recursive); mtx_destroy(&mutex); KcLockGuard lock_guard = kc_lock_guard_init(&mutex, __FILE__, __func__, __LINE__); assert_null(lock_guard.mutex); +#endif } /** diff --git a/modules/test/src/test_memory.c b/modules/test/src/test_memory.c index b8b953e..a775651 100644 --- a/modules/test/src/test_memory.c +++ b/modules/test/src/test_memory.c @@ -303,7 +303,11 @@ ptr = raw_aligned_alloc(sizeof(int), sizeof(int)); assert_not_null(ptr); +#if (KC_IS_WINDOWS) + _aligned_free(ptr); +#else raw_free(ptr); +#endif ptr = raw_calloc(10, sizeof(int)); assert_not_null(ptr); diff --git a/modules/test/src/test_memory_mark.c b/modules/test/src/test_memory_mark.c index cd518f9..dcfcf61 100644 --- a/modules/test/src/test_memory_mark.c +++ b/modules/test/src/test_memory_mark.c @@ -8,8 +8,6 @@ // プロトタイプ宣言 static void test_memory_mark_allocated(void); -static void test_memory_mark_allocated_new(void); -static void test_memory_mark_allocated_new_array(void); static void test_memory_mark_deleted(void); static void test_memory_mark_other(void); @@ -20,8 +18,6 @@ { KcUt *ut = KcUt_get_instance(); ut->add(ut, UT_TESTCASE, "memory_mark ALLOCATED", test_memory_mark_allocated); - ut->add(ut, UT_TESTCASE, "memory_mark ALLOCATED_NEW", test_memory_mark_allocated_new); - ut->add(ut, UT_TESTCASE, "memory_mark ALLOCATED_NEW_ARRAY", test_memory_mark_allocated_new_array); ut->add(ut, UT_TESTCASE, "memory_mark DELETED", test_memory_mark_deleted); ut->add(ut, UT_TESTCASE, "memory_mark OTHER", test_memory_mark_other); } @@ -39,30 +35,6 @@ } /** - * KcMemoryMark の文字列表現取得 (ALLOCATED_NEW) - * - * @process KC_MEMORY_ALLOCATED_NEW の文字列表現を取得する。 - * @result 'new ' が取得されること。 - */ -static void test_memory_mark_allocated_new(void) -{ - const char *res = KcMemoryMark_to_string(KC_MEMORY_ALLOCATED_NEW); - assert_equals("new ", res); -} - -/** - * KcMemoryMark の文字列表現取得 (ALLOCATED_NEW_ARRAY) - * - * @process KC_MEMORY_ALLOCATED_NEW_ARRAY の文字列表現を取得する。 - * @result 'new[] ' が取得されること。 - */ -static void test_memory_mark_allocated_new_array(void) -{ - const char *res = KcMemoryMark_to_string(KC_MEMORY_ALLOCATED_NEW_ARRAY); - assert_equals("new[] ", res); -} - -/** * KcMemoryMark の文字列表現取得 (ALLOCATED_DELETED) * * @process KC_MEMORY_DELETED の文字列表現を取得する。 diff --git a/config.mk b/config.mk index f7c6e47..5017c77 100644 --- a/config.mk +++ b/config.mk @@ -12,31 +12,43 @@ # # +# OS指定 +# +#OS = linux +OS = windows + +# # アーキテクチャ指定 # -ARCH ?= x86_64 -# ARCH ?= aarch64 -# ARCH ?= i686-w64-mingw32 +ARCH ?= +#ARCH ?= aarch64 +#ARCH ?= i686-w64-mingw32 # # クロスコンパイラ指定 # -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -# for Windows -CROSS_COMPILE ?=i686-w64-mingw32- +CROSS_COMPILE=$(ARCH)- + +# +# コンパイラ +# +# +#CC = $(CROSS_COMPILE)gcc +#CXX = $(CROSS_COMPILE)g++ +CC = $(CROSS_COMPILE)clang +CXX = $(CROSS_COMPILE)clang++ +#CC = $(CROSS_COMPILE)clang-16 +#CXX = $(CROSS_COMPILE)clang++16 + +# +# オプション設定 +# +ifeq ($(strip $(OS)),windows) LIBS ?= -lws2_32 DEBUG_OPTIONS ?= else -# For Linux -ifeq ($(strip $(ARCH)),aarch64) -CROSS_COMPILE ?= LIBS ?= -lpthread -lrt DEBUG_OPTIONS ?= -fstack-protector -else -CROSS_COMPILE ?= -LIBS ?= -lpthread -lrt -DEBUG_OPTIONS ?= -fstack-protector -endif endif # @@ -70,3 +82,17 @@ CFLAGS += -DKC_MEMORY_ENABLED=1 CXXFLAGS += -DKCPP_MEMORY_ENABLED=1 + +# ------------------------------------------------------------------------------ +# TARGET 調整 +# ------------------------------------------------------------------------------ +ifeq ($(strip $(OS)),windows) +ifeq ($(strip $(NAME)),$(strip $(TARGET))) +TARGET = $(NAME).exe +else +ifeq ($(strip $(NAME)).so,$(strip $(TARGET))) +TARGET = $(NAME).dll +endif +endif +endif + diff --git a/mk/base-auto.mk b/mk/base-auto.mk index ec0bc7b..1945456 100644 --- a/mk/base-auto.mk +++ b/mk/base-auto.mk @@ -4,6 +4,7 @@ # 以下、基本設定に基づき自動的に設定されます。 # (本ファイルは、基本的に変更不要です。) + # ------------------------------------------------------------------------------ # SRCS, OBJS, DEPS ファイル群の自動設定 # ------------------------------------------------------------------------------ diff --git a/mk/base-cmd.mk b/mk/base-cmd.mk index e74c114..edde6de 100644 --- a/mk/base-cmd.mk +++ b/mk/base-cmd.mk @@ -31,15 +31,8 @@ TAR = tar TOUCH = touch -CC = $(CROSS_COMPILE)gcc -CXX = $(CROSS_COMPILE)g++ - -#CC = $(CROSS_COMPILE)clang -#CXX = $(CROSS_COMPILE)clang++ - -#CC = $(CROSS_COMPILE)clang-16 -#CXX = $(CROSS_COMPILE)clang++16 - +CC ?= $(CROSS_COMPILE)gcc +CXX ?= $(CROSS_COMPILE)g++ RANLIB = $(CROSS_COMPILE)ranlib ADDR2LINE = $(CROSS_COMPILE)addr2line AR = $(CROSS_COMPILE)ar diff --git a/mk/link-dll-conf.mk b/mk/link-dll-conf.mk index c470c17..d47c5dc 100644 --- a/mk/link-dll-conf.mk +++ b/mk/link-dll-conf.mk @@ -16,6 +16,7 @@ CLEAN_FILES += $(TOP_TARGET) CLEAN_FILES += $(TOP_HEADER_FILES) +CLEAN_FILES += $(NAME).lib endif endif diff --git a/mk/link-exe-rule.mk b/mk/link-exe-rule.mk index 2f73a13..322c4a3 100644 --- a/mk/link-exe-rule.mk +++ b/mk/link-exe-rule.mk @@ -2,10 +2,20 @@ # 実行ファイル 生成ルール # ------------------------------------------------------------------------------ ifneq ($(strip $(NAME)),) + +# For Linux ifeq ($(strip $(NAME)),$(strip $(TARGET))) $(TARGET): $(OBJS) $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) endif + +# For Windows (or ut.exe) +ifeq ($(strip $(NAME).exe),$(strip $(TARGET))) +$(TARGET): $(OBJS) + $(LINK) $(LDFLAGS) -o $@ $^ $(LIBS) + +endif + endif diff --git a/modules/Makefile b/modules/Makefile index dca3d67..99c9881 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -11,7 +11,7 @@ TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk NAME = libkc -TARGET = $(NAME) +TARGET = $(NAME).exe SUBDIRS = test USE_SO_VERSION = diff --git a/modules/include/kc_memory.h b/modules/include/kc_memory.h index c6ba9e9..82c24bd 100644 --- a/modules/include/kc_memory.h +++ b/modules/include/kc_memory.h @@ -207,7 +207,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); void *(*_reallocate_managed_ptr)(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); - void (*_deallocate)(void *ptr, KcMemoryMark expected_mark); + void (*_deallocate)(void *ptr); // ========================================================================= // 内部利用変数 @@ -236,6 +236,7 @@ /** * stdlib.h の aligned_alloc + * windows の場合、本関数で獲得したメモリは、_aligned_free で解放してください。 * * @param alignment アライメント * @param size 確保するメモリサイズ diff --git a/modules/include/kc_memory_entry.h b/modules/include/kc_memory_entry.h index 633b161..8b183e5 100644 --- a/modules/include/kc_memory_entry.h +++ b/modules/include/kc_memory_entry.h @@ -34,6 +34,7 @@ int line; //!< メモリ確保行番号 struct KcMemoryEntry_ *_prev; //!< 前の管理メモリポインタ struct KcMemoryEntry_ *_next; //!< 次の管理メモリポインタ + void *padding[2]; //!< パディング void *data; //!< データ // 構造体末尾の配列に限りサイズ省略可能 (C99 : incomplete array) } KcMemoryEntry; diff --git a/modules/include/kc_memory_mark.h b/modules/include/kc_memory_mark.h index cf62c68..7c34e7e 100644 --- a/modules/include/kc_memory_mark.h +++ b/modules/include/kc_memory_mark.h @@ -20,15 +20,24 @@ using namespace std; #endif +/** + * 指定された KcMemoryMark の値が、管理メモリか否かを判定します。 + * + * @param mark 判定する KcMemoryMark + * @return true/false (管理メモリ/管理メモリ外) + */ +#define KC_MEMORY_IS_MANAGED(mark) ((mark & 0xFFFF0000) == 0x55AA0000) + /** * メモリ状態 */ typedef enum { - KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み - KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み - KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み - KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み + KC_MEMORY_DELETED = 0x55AA0000, //!< 解放済み + KC_MEMORY_ALLOCATED = 0x55AA1111, //!< 確保済み + KC_MEMORY_ALLOCATED_ALIGNED = 0x55AA1155 //!< 確保済み(aligned) + // KC_MEMORY_ALLOCATED_NEW = 0x55AA2222, //!< new により確保済み + // KC_MEMORY_ALLOCATED_NEW_ARRAY = 0x55AA4444 //!< new[] により確保済み } KcMemoryMark; /** diff --git a/modules/src/kc_env.c b/modules/src/kc_env.c index 95f185c..cb0965a 100644 --- a/modules/src/kc_env.c +++ b/modules/src/kc_env.c @@ -10,7 +10,8 @@ #include #include -#define KC_ENV_BUFFER_MAX (4096) +// Windows 環境変数の最大長は 327676 +#define KC_ENV_BUFFER_MAX (32767) /** * 指定された環境変数の値を取得します。 @@ -50,7 +51,7 @@ char *result = NULL; #if (KC_IS_WINDOWS) static char buff[KC_ENV_BUFFER_MAX]; - DWORD ret = GetEnvironmentVariable(key_name, (LPSTR)buff, sizeof(buff)); + DWORD ret = GetEnvironmentVariable(key_name, buff, sizeof(buff)); if (ret != 0) { result = buff; @@ -81,10 +82,10 @@ #if (KC_IS_WINDOWS) // = が含まれている場合はエラー char *ptr = strstr(name, "="); - if (ptr) + if (ptr == NULL) { - LPSTR buff[1]; - DWORD ret = GetEnvironmentVariable(name, (LPSTR)buff, 1); + char buff[1]; + DWORD ret = GetEnvironmentVariable(name, buff, 1); if ((!overwrite) && (ret != 0)) { // 上書きなし && 既に環境変数に存在するので true を返す。 return true; diff --git a/modules/src/kc_list_linked.c b/modules/src/kc_list_linked.c index 47edbe4..e4a7d84 100644 --- a/modules/src/kc_list_linked.c +++ b/modules/src/kc_list_linked.c @@ -22,13 +22,6 @@ #include #include -#if defined(__GNUC__) -#define _GNU_SOURCE 1 -#if !(KC_IS_WINDOWS) -#define qsort_s qsort_r -#endif // KC_IS_WINDOWS -#endif // defined(__GNUC__) - /** * KcLinkedList Entry 情報 */ @@ -369,7 +362,9 @@ } prev_entry->next = info->tail; info->tail->prev = prev_entry; + printf("---free(ptr_list) %p---\n", ptr_list); free(ptr_list); + printf("---end---\n"); } } diff --git a/modules/src/kc_memory.c b/modules/src/kc_memory.c index 6dbbc88..2c4471c 100644 --- a/modules/src/kc_memory.c +++ b/modules/src/kc_memory.c @@ -49,7 +49,7 @@ KcMemoryMark mark, const char *file, const char *func, int line); static void *KcMemoryManager_reallocate_managed_ptr(void *ptr, size_t size, KcMemoryMark mark, const char *file, const char *func, int line); -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark); +static void KcMemoryManager_deallocate(void *ptr); //////////////////////////////////////////////////////////////////////////////// // @@ -326,7 +326,7 @@ */ static void *KcMemoryManager_aligned_alloc(size_t alignment, size_t size, const char *file, const char *func, int line) { - void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(alignment, size, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); return ptr; } @@ -344,7 +344,7 @@ static void *KcMemoryManager_calloc(size_t nmemb, size_t size, const char *file, const char *func, int line) { size_t n = nmemb * size; - void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED, file, func, line); + void *ptr = kc_memory_manager->_allocate(size, n, KC_MEMORY_ALLOCATED_ALIGNED, file, func, line); if (ptr != NULL) { memset(ptr, 0x00, n); @@ -375,7 +375,7 @@ */ static void KcMemoryManager_free(void *ptr) { // malloc, calloc 等で確保されたメモリを解放する。 - kc_memory_manager->_deallocate(ptr, KC_MEMORY_ALLOCATED); + kc_memory_manager->_deallocate(ptr); } // ------------------------------------- @@ -579,9 +579,8 @@ * 指定されたポインタのメモリを解放します。 * * @param ptr 解放するメモリへのポインタ - * @param expected_mark 期待するメモリ状態 */ -static void KcMemoryManager_deallocate(void *ptr, KcMemoryMark expected_mark) +static void KcMemoryManager_deallocate(void *ptr) { if (ptr == NULL) { @@ -590,14 +589,14 @@ KcMemoryEntry *entry = (KcMemoryEntry *)ptr; entry--; - if (entry->mark == expected_mark) - { // 期待するメモリ状態の場合、そのまま解放する。 + if (KC_MEMORY_IS_MANAGED(entry->mark)) + { // 管理メモリの場合、エントリ解放する。 kc_memory_manager->_listener.free(entry); kc_memory_manager->_remove(entry); KcMemoryEntry_delete(entry); } else - { // 期待通りでない場合、管理外メモリのため stdlib.h の free にて解放する。 + { // 、管理外メモリのため stdlib.h の free にて解放する。 raw_free(ptr); } } diff --git a/modules/src/kc_memory_entry.c b/modules/src/kc_memory_entry.c index 85f3fc2..78139a8 100644 --- a/modules/src/kc_memory_entry.c +++ b/modules/src/kc_memory_entry.c @@ -18,9 +18,6 @@ // 定数定義 // -/** パディング */ -#define KC_MEMORY_PADDING (sizeof(void *) * 2) - //////////////////////////////////////////////////////////////////////////////// // // 変数定義 @@ -59,18 +56,18 @@ } #endif KcMemoryEntry *new_entry; - if ((entry == NULL) && (alignment > 0)) + if ((entry == NULL) && (alignment > 0) && (mark == KC_MEMORY_ALLOCATED_ALIGNED)) { // アライメント指定でメモリを確保する。 - new_entry = (KcMemoryEntry *)raw_aligned_alloc(alignment, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_aligned_alloc( + alignment, (size_t)(sizeof(KcMemoryEntry) + size)); } else { - new_entry = (KcMemoryEntry *)raw_realloc(entry, - (size_t)(size + sizeof(KcMemoryEntry) + KC_MEMORY_PADDING)); + new_entry = (KcMemoryEntry *)raw_realloc( + entry, (size_t)(sizeof(KcMemoryEntry) + size)); } - KcMemoryEntry_set(new_entry, size, mark, file, func, line); + return new_entry; } @@ -81,9 +78,18 @@ */ void KcMemoryEntry_delete(KcMemoryEntry *entry) { - entry->mark = KC_MEMORY_DELETED; - entry->size = 0; - raw_free(entry); + if (KC_IS_WINDOWS && (entry->mark == KC_MEMORY_ALLOCATED_ALIGNED)) + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + _aligned_free(entry); + } + else + { + entry->mark = KC_MEMORY_DELETED; + entry->size = 0; + raw_free(entry); + } } /** diff --git a/modules/src/kc_memory_mark.c b/modules/src/kc_memory_mark.c index ba246b2..d68b56c 100644 --- a/modules/src/kc_memory_mark.c +++ b/modules/src/kc_memory_mark.c @@ -26,10 +26,8 @@ return "delete"; case KC_MEMORY_ALLOCATED: return "alloc "; - case KC_MEMORY_ALLOCATED_NEW: - return "new "; - case KC_MEMORY_ALLOCATED_NEW_ARRAY: - return "new[] "; + case KC_MEMORY_ALLOCATED_ALIGNED: + return "alloca"; default: return "other "; } diff --git a/modules/src/kc_queue.c b/modules/src/kc_queue.c index d9f2a14..2df6e42 100644 --- a/modules/src/kc_queue.c +++ b/modules/src/kc_queue.c @@ -82,6 +82,7 @@ KcQueueInfo *info = (KcQueueInfo *)queue->_info; info->list = list; info->queue_size = (size == 0) ? INT_MAX : size; + printf("queue info->mutex: %p\n", &(info->mutex)); mtx_init(&(info->mutex), mtx_plain); cnd_init(&(info->not_empty)); cnd_init(&(info->not_full)); @@ -319,8 +320,10 @@ */ static void KcQueue_cleanup_info(struct KcQueue_ *queue) { + printf("### cleanup info\n"); KcQueueInfo *info = (KcQueueInfo *)queue->_info; KcList_delete(info->list); + printf("info->mutex : %p\n", &info->mutex); mtx_destroy(&(info->mutex)); cnd_destroy(&(info->not_empty)); cnd_destroy(&(info->not_full)); diff --git a/modules/src/kc_threads_win.c b/modules/src/kc_threads_win.c index 0439fda..c942ceb 100644 --- a/modules/src/kc_threads_win.c +++ b/modules/src/kc_threads_win.c @@ -3,6 +3,8 @@ * @brief スレッドモジュール * @copyright 2020 - 2024 Nomura Kei */ +#include + #include #include @@ -100,6 +102,14 @@ return thrd_success; } +/** + * 指定されたミューテックスを初期化します。 + * Windows の場合、常に mtx_recursive が有効となります。 + * + * @param mtx 初期化するミューテックスの識別子 + * @param type タイプ (mtx_plain, mtx_timed, mtx_plain|mtx_recursive, mtx_timed|mtx_recursive) + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_init(mtx_t *mtx, int type) { if (mtx == NULL) @@ -107,30 +117,36 @@ return thrd_error; } mtx->type = type; - if (type & mtx_plain) - { - InitializeCriticalSection(&mtx->cs); - } - else if (type & mtx_recursive) - { - InitializeCriticalSection(&mtx->cs); - } - else - { - return thrd_error; - } + InitializeCriticalSection(&mtx->cs); return thrd_success; } +/** + * ミューテックスを破棄します。 + * + * @param mtx 破棄するミューテックス識別子 + */ void mtx_destroy(mtx_t *mtx) { + printf("mtx_destroy: mtx->cs:%p\n", &mtx->cs); DeleteCriticalSection(&mtx->cs); } + +/** + * 指定されたミューテックスがロックされるまで現在のスレッドをロックします。 + * + * @param mtx ミューテックス識別子 + * @return thrd_success/thrd_error (成功/失敗) + */ int mtx_lock(mtx_t *mtx) { EnterCriticalSection(&mtx->cs); return thrd_success; } + +/** + * + */ int mtx_unlock(mtx_t *mtx) { LeaveCriticalSection(&mtx->cs); diff --git a/modules/test/Makefile b/modules/test/Makefile index 29cc50b..37f67b2 100644 --- a/modules/test/Makefile +++ b/modules/test/Makefile @@ -10,7 +10,7 @@ # TOPDIR ?= ../.. RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe +NAME = ut TARGET = $(NAME) SUBDIRS = USE_SO_VERSION = diff --git a/modules/test/src/test_env.c b/modules/test/src/test_env.c index 0321563..a88d6e1 100644 --- a/modules/test/src/test_env.c +++ b/modules/test/src/test_env.c @@ -24,7 +24,6 @@ { // 事前設定 KcEnv_remove("UT_TEST_ENV"); - // 値設定 bool ret = KcEnv_set("UT_TEST_ENV", "ABC", false); assert_true(ret); @@ -33,11 +32,12 @@ ret = KcEnv_set("UT_TEST_ENV", "XYZ", false); assert_true(ret); char *env_val = KcEnv_get("UT_TEST_ENV"); - assert_equals("ABC", env_val); + assert_equals((const char *)"ABC", env_val); // 上書きあり、既に存在する。 ret = KcEnv_set("UT_TEST_ENV", "XYZ", true); assert_true(ret); + printf("--6--\n"); env_val = KcEnv_get("UT_TEST_ENV"); assert_equals("XYZ", env_val); diff --git a/modules/test/src/test_lock_guard.c b/modules/test/src/test_lock_guard.c index 9c3249f..378646d 100644 --- a/modules/test/src/test_lock_guard.c +++ b/modules/test/src/test_lock_guard.c @@ -66,11 +66,14 @@ */ static void test_lock_guard_init_invalid_mutex(void) { +#if !(KC_IS_WINDOWS) + // Windows の場合は、エラー応答でなく落ちる。 mtx_t mutex; mtx_init(&mutex, mtx_plain | mtx_recursive); mtx_destroy(&mutex); KcLockGuard lock_guard = kc_lock_guard_init(&mutex, __FILE__, __func__, __LINE__); assert_null(lock_guard.mutex); +#endif } /** diff --git a/modules/test/src/test_memory.c b/modules/test/src/test_memory.c index b8b953e..a775651 100644 --- a/modules/test/src/test_memory.c +++ b/modules/test/src/test_memory.c @@ -303,7 +303,11 @@ ptr = raw_aligned_alloc(sizeof(int), sizeof(int)); assert_not_null(ptr); +#if (KC_IS_WINDOWS) + _aligned_free(ptr); +#else raw_free(ptr); +#endif ptr = raw_calloc(10, sizeof(int)); assert_not_null(ptr); diff --git a/modules/test/src/test_memory_mark.c b/modules/test/src/test_memory_mark.c index cd518f9..dcfcf61 100644 --- a/modules/test/src/test_memory_mark.c +++ b/modules/test/src/test_memory_mark.c @@ -8,8 +8,6 @@ // プロトタイプ宣言 static void test_memory_mark_allocated(void); -static void test_memory_mark_allocated_new(void); -static void test_memory_mark_allocated_new_array(void); static void test_memory_mark_deleted(void); static void test_memory_mark_other(void); @@ -20,8 +18,6 @@ { KcUt *ut = KcUt_get_instance(); ut->add(ut, UT_TESTCASE, "memory_mark ALLOCATED", test_memory_mark_allocated); - ut->add(ut, UT_TESTCASE, "memory_mark ALLOCATED_NEW", test_memory_mark_allocated_new); - ut->add(ut, UT_TESTCASE, "memory_mark ALLOCATED_NEW_ARRAY", test_memory_mark_allocated_new_array); ut->add(ut, UT_TESTCASE, "memory_mark DELETED", test_memory_mark_deleted); ut->add(ut, UT_TESTCASE, "memory_mark OTHER", test_memory_mark_other); } @@ -39,30 +35,6 @@ } /** - * KcMemoryMark の文字列表現取得 (ALLOCATED_NEW) - * - * @process KC_MEMORY_ALLOCATED_NEW の文字列表現を取得する。 - * @result 'new ' が取得されること。 - */ -static void test_memory_mark_allocated_new(void) -{ - const char *res = KcMemoryMark_to_string(KC_MEMORY_ALLOCATED_NEW); - assert_equals("new ", res); -} - -/** - * KcMemoryMark の文字列表現取得 (ALLOCATED_NEW_ARRAY) - * - * @process KC_MEMORY_ALLOCATED_NEW_ARRAY の文字列表現を取得する。 - * @result 'new[] ' が取得されること。 - */ -static void test_memory_mark_allocated_new_array(void) -{ - const char *res = KcMemoryMark_to_string(KC_MEMORY_ALLOCATED_NEW_ARRAY); - assert_equals("new[] ", res); -} - -/** * KcMemoryMark の文字列表現取得 (ALLOCATED_DELETED) * * @process KC_MEMORY_DELETED の文字列表現を取得する。 diff --git a/modules/test/test-lib/Makefile b/modules/test/test-lib/Makefile index 9de07ae..9327f53 100644 --- a/modules/test/test-lib/Makefile +++ b/modules/test/test-lib/Makefile @@ -11,12 +11,7 @@ TOPDIR ?= ../../.. RULEDIR ?= $(TOPDIR)/mk NAME = libtest - -ifeq ($(strip $(ARCH)),i686-w64-mingw32) -TARGET = $(NAME).dll -else TARGET = $(NAME).so -endif SUBDIRS = USE_SO_VERSION =