better memory tracking
This commit is contained in:
parent
9a4f2225ce
commit
439f43fc52
2 changed files with 64 additions and 55 deletions
|
|
@ -9,20 +9,6 @@
|
||||||
|
|
||||||
## Memory tracking support for Nim.
|
## Memory tracking support for Nim.
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
import db_sqlite
|
|
||||||
var db = open("memtrack.db", "", "", "")
|
|
||||||
db.exec sql"""
|
|
||||||
create table if not exists Tracking(
|
|
||||||
id integer primary key,
|
|
||||||
op varchar not null,
|
|
||||||
address integer not null,
|
|
||||||
size integer not null,
|
|
||||||
file varchar not null,
|
|
||||||
line integer not null
|
|
||||||
)"""
|
|
||||||
db.close()
|
|
||||||
else:
|
|
||||||
when not defined(memTracker):
|
when not defined(memTracker):
|
||||||
{.error: "Memory tracking support is turned off!".}
|
{.error: "Memory tracking support is turned off!".}
|
||||||
|
|
||||||
|
|
@ -61,11 +47,32 @@ else:
|
||||||
if not success:
|
if not success:
|
||||||
quit "could not write to database!"
|
quit "could not write to database!"
|
||||||
|
|
||||||
|
proc execQuery(q: string) =
|
||||||
|
var s: Pstmt
|
||||||
|
if prepare_v2(dbHandle, q, q.len.int32, s, nil) == SQLITE_OK:
|
||||||
|
discard step(s)
|
||||||
|
if finalize(s) != SQLITE_OK:
|
||||||
|
quit "could not finalize " & $sqlite3.errmsg(dbHandle)
|
||||||
|
else:
|
||||||
|
quit "could not prepare statement " & $sqlite3.errmsg(dbHandle)
|
||||||
|
|
||||||
|
proc setupDb() =
|
||||||
|
execQuery """create table if not exists tracking(
|
||||||
|
id integer primary key,
|
||||||
|
op varchar not null,
|
||||||
|
address integer not null,
|
||||||
|
size integer not null,
|
||||||
|
file varchar not null,
|
||||||
|
line integer not null
|
||||||
|
)"""
|
||||||
|
execQuery "delete from tracking"
|
||||||
|
|
||||||
if sqlite3.open("memtrack.db", dbHandle) == SQLITE_OK:
|
if sqlite3.open("memtrack.db", dbHandle) == SQLITE_OK:
|
||||||
|
setupDb()
|
||||||
const query = "INSERT INTO tracking(op, address, size, file, line) values (?, ?, ?, ?, ?)"
|
const query = "INSERT INTO tracking(op, address, size, file, line) values (?, ?, ?, ?, ?)"
|
||||||
if prepare_v2(dbHandle, query,
|
if prepare_v2(dbHandle, query,
|
||||||
query.len, insertStmt, nil) == SQLITE_OK:
|
query.len, insertStmt, nil) == SQLITE_OK:
|
||||||
setTrackLogger logEntries
|
setTrackLogger logEntries
|
||||||
else:
|
else:
|
||||||
quit "could not prepare statement"
|
quit "could not prepare statement B " & $sqlite3.errmsg(dbHandle)
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
|
||||||
|
|
@ -1309,10 +1309,12 @@ proc join*[T: not string](a: openArray[T], sep: string = ""): string {.
|
||||||
type
|
type
|
||||||
SkipTable = array[char, int]
|
SkipTable = array[char, int]
|
||||||
|
|
||||||
|
{.push profiler: off.}
|
||||||
proc preprocessSub(sub: string, a: var SkipTable) =
|
proc preprocessSub(sub: string, a: var SkipTable) =
|
||||||
var m = len(sub)
|
var m = len(sub)
|
||||||
for i in 0..0xff: a[chr(i)] = m+1
|
for i in 0..0xff: a[chr(i)] = m+1
|
||||||
for i in 0..m-1: a[sub[i]] = m-i
|
for i in 0..m-1: a[sub[i]] = m-i
|
||||||
|
{.pop.}
|
||||||
|
|
||||||
proc findAux(s, sub: string, start: int, a: SkipTable): int =
|
proc findAux(s, sub: string, start: int, a: SkipTable): int =
|
||||||
# Fast "quick search" algorithm:
|
# Fast "quick search" algorithm:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue