Tweaked general setup tracking allocator and added logger #11

Merged
zack merged 1 commits from example-cleanup into master 2026-04-22 06:03:11 +00:00
4 changed files with 141 additions and 62 deletions
Showing only changes of commit a157615a43 - Show all commits

View File

@@ -1,19 +1,18 @@
package examples package examples
import "core:fmt" import "core:fmt"
import "core:log"
import "core:mem" import "core:mem"
import "core:os" import "core:os"
main :: proc() { main :: proc() {
//----- Tracking allocator ---------------------------------- //----- General setup ----------------------------------
{ {
tracking_temp_allocator := false
// Temp // Temp
track_temp: mem.Tracking_Allocator track_temp: mem.Tracking_Allocator
if tracking_temp_allocator { mem.tracking_allocator_init(&track_temp, context.temp_allocator)
mem.tracking_allocator_init(&track_temp, context.temp_allocator) context.temp_allocator = mem.tracking_allocator(&track_temp)
context.temp_allocator = mem.tracking_allocator(&track_temp)
}
// Default // Default
track: mem.Tracking_Allocator track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator) mem.tracking_allocator_init(&track, context.allocator)
@@ -22,18 +21,10 @@ main :: proc() {
// This could be fine for some global state or it could be a memory leak. // This could be fine for some global state or it could be a memory leak.
defer { defer {
// Temp allocator // Temp allocator
if tracking_temp_allocator { if len(track_temp.bad_free_array) > 0 {
if len(track_temp.allocation_map) > 0 { fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
fmt.eprintf("=== %v allocations not freed - temp allocator: ===\n", len(track_temp.allocation_map)) for entry in track_temp.bad_free_array {
for _, entry in track_temp.allocation_map { fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
}
}
if len(track_temp.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
for entry in track_temp.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
} }
mem.tracking_allocator_destroy(&track_temp) mem.tracking_allocator_destroy(&track_temp)
} }
@@ -52,6 +43,9 @@ main :: proc() {
} }
mem.tracking_allocator_destroy(&track) mem.tracking_allocator_destroy(&track)
} }
// Logger
context.logger = log.create_console_logger()
defer log.destroy_console_logger(context.logger)
} }
args := os.args args := os.args

View File

@@ -1,6 +1,8 @@
package meta package meta
import "core:fmt" import "core:fmt"
import "core:log"
import "core:mem"
import "core:os" import "core:os"
Command :: struct { Command :: struct {
@@ -20,6 +22,48 @@ COMMANDS :: []Command {
} }
main :: proc() { main :: proc() {
//----- General setup ----------------------------------
when ODIN_DEBUG {
// Temp
track_temp: mem.Tracking_Allocator
mem.tracking_allocator_init(&track_temp, context.temp_allocator)
context.temp_allocator = mem.tracking_allocator(&track_temp)
// Default
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
// Log a warning about any memory that was not freed by the end of the program.
// This could be fine for some global state or it could be a memory leak.
defer {
// Temp allocator
if len(track_temp.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
for entry in track_temp.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
mem.tracking_allocator_destroy(&track_temp)
}
// Default allocator
if len(track.allocation_map) > 0 {
fmt.eprintf("=== %v allocations not freed - main allocator: ===\n", len(track.allocation_map))
for _, entry in track.allocation_map {
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
}
}
if len(track.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees - main allocator: ===\n", len(track.bad_free_array))
for entry in track.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
}
mem.tracking_allocator_destroy(&track)
}
// Logger
context.logger = log.create_console_logger()
defer log.destroy_console_logger(context.logger)
}
args := os.args[1:] args := os.args[1:]
if len(args) == 0 { if len(args) == 0 {

View File

@@ -1,39 +1,32 @@
package examples package examples
import "core:fmt" import "core:fmt"
import "core:log"
import "core:mem" import "core:mem"
import "core:os" import "core:os"
import qr ".." import qr ".."
main :: proc() { main :: proc() {
//----- Tracking allocator ---------------------------------- //----- General setup ----------------------------------
{ {
tracking_temp_allocator := false
// Temp // Temp
track_temp: mem.Tracking_Allocator track_temp: mem.Tracking_Allocator
if tracking_temp_allocator { mem.tracking_allocator_init(&track_temp, context.temp_allocator)
mem.tracking_allocator_init(&track_temp, context.temp_allocator) context.temp_allocator = mem.tracking_allocator(&track_temp)
context.temp_allocator = mem.tracking_allocator(&track_temp)
}
// Default // Default
track: mem.Tracking_Allocator track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator) mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track) context.allocator = mem.tracking_allocator(&track)
// Log a warning about any memory that was not freed by the end of the program.
// This could be fine for some global state or it could be a memory leak.
defer { defer {
// Temp allocator // Temp allocator
if tracking_temp_allocator { if len(track_temp.bad_free_array) > 0 {
if len(track_temp.allocation_map) > 0 { fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
fmt.eprintf("=== %v allocations not freed - temp allocator: ===\n", len(track_temp.allocation_map)) for entry in track_temp.bad_free_array {
for _, entry in track_temp.allocation_map { fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
}
}
if len(track_temp.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
for entry in track_temp.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
} }
mem.tracking_allocator_destroy(&track_temp) mem.tracking_allocator_destroy(&track_temp)
} }
@@ -52,6 +45,9 @@ main :: proc() {
} }
mem.tracking_allocator_destroy(&track) mem.tracking_allocator_destroy(&track)
} }
// Logger
context.logger = log.create_console_logger()
defer log.destroy_console_logger(context.logger)
} }
args := os.args args := os.args

View File

@@ -1,8 +1,11 @@
package examples package examples
import "core:fmt" import "core:fmt"
import "core:log"
import "core:mem"
import "core:os" import "core:os"
import "core:sys/posix" import "core:sys/posix"
import mdb "../../lmdb" import mdb "../../lmdb"
// 0o660 // 0o660
@@ -10,33 +13,75 @@ DB_MODE :: posix.mode_t{.IWGRP, .IRGRP, .IWUSR, .IRUSR}
DB_PATH :: "out/debug/lmdb_example_db" DB_PATH :: "out/debug/lmdb_example_db"
main :: proc() { main :: proc() {
environment: ^mdb.Env //----- General setup ----------------------------------
{
// Temp
track_temp: mem.Tracking_Allocator
mem.tracking_allocator_init(&track_temp, context.temp_allocator)
context.temp_allocator = mem.tracking_allocator(&track_temp)
// Create environment for lmdb // Default
mdb.panic_on_err(mdb.env_create(&environment)) track: mem.Tracking_Allocator
// Create directory for databases. Won't do anything if it already exists. mem.tracking_allocator_init(&track, context.allocator)
os.make_directory(DB_PATH) context.allocator = mem.tracking_allocator(&track)
// Open the database files (creates them if they don't already exist) // Log a warning about any memory that was not freed by the end of the program.
mdb.panic_on_err(mdb.env_open(environment, DB_PATH, {}, DB_MODE)) // This could be fine for some global state or it could be a memory leak.
defer {
// Temp allocator
if len(track_temp.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
for entry in track_temp.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
mem.tracking_allocator_destroy(&track_temp)
}
// Default allocator
if len(track.allocation_map) > 0 {
fmt.eprintf("=== %v allocations not freed - main allocator: ===\n", len(track.allocation_map))
for _, entry in track.allocation_map {
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
}
}
if len(track.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees - main allocator: ===\n", len(track.bad_free_array))
for entry in track.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
}
mem.tracking_allocator_destroy(&track)
}
// Logger
context.logger = log.create_console_logger()
defer log.destroy_console_logger(context.logger)
}
// Transactions environment: ^mdb.Env
txn_handle: ^mdb.Txn
db_handle: mdb.Dbi
// Put transaction
key := 7
key_val := mdb.blittable_val(&key)
put_data := 12
put_data_val := mdb.blittable_val(&put_data)
mdb.panic_on_err(mdb.txn_begin(environment, nil, {}, &txn_handle))
mdb.panic_on_err(mdb.dbi_open(txn_handle, nil, {}, &db_handle))
mdb.panic_on_err(mdb.put(txn_handle, db_handle, &key_val, &put_data_val, {}))
mdb.panic_on_err(mdb.txn_commit(txn_handle))
// Get transaction // Create environment for lmdb
data_val: mdb.Val mdb.panic_on_err(mdb.env_create(&environment))
mdb.panic_on_err(mdb.txn_begin(environment, nil, {}, &txn_handle)) // Create directory for databases. Won't do anything if it already exists.
mdb.panic_on_err(mdb.get(txn_handle, db_handle, &key_val, &data_val)) os.make_directory(DB_PATH)
data_cpy := mdb.blittable_copy(&data_val, int) // Open the database files (creates them if they don't already exist)
mdb.panic_on_err(mdb.txn_commit(txn_handle)) mdb.panic_on_err(mdb.env_open(environment, DB_PATH, {}, DB_MODE))
fmt.println("Get result:", data_cpy)
// Transactions
txn_handle: ^mdb.Txn
db_handle: mdb.Dbi
// Put transaction
key := 7
key_val := mdb.blittable_val(&key)
put_data := 12
put_data_val := mdb.blittable_val(&put_data)
mdb.panic_on_err(mdb.txn_begin(environment, nil, {}, &txn_handle))
mdb.panic_on_err(mdb.dbi_open(txn_handle, nil, {}, &db_handle))
mdb.panic_on_err(mdb.put(txn_handle, db_handle, &key_val, &put_data_val, {}))
mdb.panic_on_err(mdb.txn_commit(txn_handle))
// Get transaction
data_val: mdb.Val
mdb.panic_on_err(mdb.txn_begin(environment, nil, {}, &txn_handle))
mdb.panic_on_err(mdb.get(txn_handle, db_handle, &key_val, &data_val))
data_cpy := mdb.blittable_copy(&data_val, int)
mdb.panic_on_err(mdb.txn_commit(txn_handle))
fmt.println("Get result:", data_cpy)
} }