Skip to content

Commit

Permalink
Add cli option to enable treesitter mem tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 22, 2024
1 parent 811b5b8 commit 3ed5e61
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
5 changes: 5 additions & 0 deletions src/desktop_main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ else:
import std/[parseopt, options, macros, strutils, os, terminal, strformat]
import misc/[custom_logger, util]
import compilation_config, scripting_api, app_options
import text/custom_treesitter

proc tryAttach(opts: AppOptions, processId: int)

Expand Down Expand Up @@ -70,6 +71,7 @@ Options:
-s, --session Load a specific session.
--attach Open the passed files in an existing instance if it already exists.
--clean Don't load any configs/sessions/plugins
--ts-mem-tracking Enable treesitter memory tracking (for debugging)
Examples:
nev Open .{appName}-session if it exists
Expand Down Expand Up @@ -153,6 +155,9 @@ block: ## Parse command line options
of "session", "s":
opts.sessionOverride = val.some

of "ts-mem-tracking":
enableTreesitterMemoryTracking()

of cmdEnd: assert(false) # cannot happen

if attach.getSome(attach):
Expand Down
84 changes: 42 additions & 42 deletions src/text/custom_treesitter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -801,51 +801,51 @@ proc freeDynamicLibraries*() =
var tsAllocated*: uint64 = 0
var tsFreed*: uint64 = 0

when defined(tsMeasureAllocs):
proc tsMalloc(a1: csize_t): pointer {.stdcall.} =
tsAllocated += a1.uint64
let p = allocShared0(a1 + 8)
if p == nil:
return nil

cast[ptr uint64](p)[] = a1.uint64
return cast[pointer](cast[uint64](p) + 8)

proc tsCalloc(a1: csize_t; a2: csize_t): pointer {.stdcall.} =
let size = a1.uint64 * a2.uint64
tsAllocated += size
let p = allocShared0(size + 8)
if p == nil:
return nil

cast[ptr uint64](p)[] = size
return cast[pointer](cast[uint64](p) + 8)

proc tsRealloc(a1: pointer; a2: csize_t): pointer {.stdcall.} =
if a1 == nil:
return tsMalloc(a2)

let original = cast[ptr uint64](cast[uint64](a1) - 8)
let size = original[]
if size > a2:
tsFreed += size - a2.uint64
else:
tsAllocated += a2.uint64 - size
proc tsMalloc(a1: csize_t): pointer {.stdcall.} =
tsAllocated += a1.uint64
let p = allocShared0(a1 + 8)
if p == nil:
return nil

cast[ptr uint64](p)[] = a1.uint64
return cast[pointer](cast[uint64](p) + 8)

proc tsCalloc(a1: csize_t; a2: csize_t): pointer {.stdcall.} =
let size = a1.uint64 * a2.uint64
tsAllocated += size
let p = allocShared0(size + 8)
if p == nil:
return nil

cast[ptr uint64](p)[] = size
return cast[pointer](cast[uint64](p) + 8)

proc tsRealloc(a1: pointer; a2: csize_t): pointer {.stdcall.} =
if a1 == nil:
return tsMalloc(a2)

let original = cast[ptr uint64](cast[uint64](a1) - 8)
let size = original[]
if size > a2:
tsFreed += size - a2.uint64
else:
tsAllocated += a2.uint64 - size

let p = reallocShared(original, a2 + 8)
if p == nil:
return nil
let p = reallocShared(original, a2 + 8)
if p == nil:
return nil

cast[ptr uint64](p)[] = a2.uint64
return cast[pointer](cast[uint64](p) + 8)
cast[ptr uint64](p)[] = a2.uint64
return cast[pointer](cast[uint64](p) + 8)

proc tsFree(a1: pointer) {.stdcall.} =
if a1 == nil:
return
proc tsFree(a1: pointer) {.stdcall.} =
if a1 == nil:
return

let original = cast[ptr uint64](cast[uint64](a1) - 8)
let size = original[]
tsFreed += size.uint64
deallocShared(original)
let original = cast[ptr uint64](cast[uint64](a1) - 8)
let size = original[]
tsFreed += size.uint64
deallocShared(original)

proc enableTreesitterMemoryTracking*() =
tsSetAllocator(tsMalloc, tsCalloc, tsRealloc, tsFree)

0 comments on commit 3ed5e61

Please sign in to comment.