Skip to main content

ideas of memory pool

· One min read

the key information to implement a memory pool

  • boundary:

How to solve the boundaries between different different chunks

  • name:

How to get an allocated memory chunk

  • reference count

whether the chunk is still used. I want to free the memory only if the process termination or free, with threads free, only do reference count minus 1.

  • data structure:

which data structure will be efficient?

memory leak

· One min read

find memory leak of a running process

$ cat /proc/$pid/smaps
  1. find out the PID of the process
$ ps -aux
  1. capture /proc/PID/smaps and save into some file like before_meminc.txt
  2. wait till memory gets increased
  3. try again step 2
  4. find the difference between first smaps and 2nd smaps, e.g. with
$ diff -u before_meminc.txt after_meminc.txt
  1. note down the address range where memory got increased

  2. use pstack and watch command to get the right call stack

$ watch -n 1 'pstack $PID | tee -a $PID.stack'

C-c when we caputred right stack

  1. check our stack file, find the functions between address range which we got from step 6.

gcc error when inline without static

· 3 min read

gcc error when inline without static

inline only

C code:

#include <stdio.h>

inline int func() { printf("dll\n"); }

int main() { func(); }

asm code:

  .section	__TEXT,__text,regular,pure_instructions
.build_version macos, 11, 0 sdk_version 11, 1
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
callq _func
xorl %ecx, %ecx
movl %eax, -4(%rbp) ## 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
## -- End function
.subsections_via_symbols

With static

C code

#include <stdio.h>

static inline int func() { printf("dll\n"); }

int main() { func(); }

asm code

  .section	__TEXT,__text,regular,pure_instructions
.build_version macos, 11, 0 sdk_version 11, 1
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
callq _func
xorl %ecx, %ecx
movl %eax, -4(%rbp) ## 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
## -- End function
.p2align 4, 0x90 ## -- Begin function func
_func: ## @func
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
leaq L_.str(%rip), %rdi
movb $0, %al
callq _printf
movl -4(%rbp), %ecx
movl %eax, -8(%rbp) ## 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
## -- End function
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "dll\n"

.subsections_via_symbols

With __attribute__

C code

#include <stdio.h>

#define inline __attribute__((alwyas_inline))

inline int func() { printf("dll\n"); }

int main() { func(); }

asm code

  .section	__TEXT,__text,regular,pure_instructions
.build_version macos, 11, 0 sdk_version 11, 1
.globl _func ## -- Begin function func
.p2align 4, 0x90
_func: ## @func
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
leaq L_.str(%rip), %rdi
movb $0, %al
callq _printf
movl -4(%rbp), %ecx
movl %eax, -8(%rbp) ## 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
## -- End function
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
callq _func
xorl %ecx, %ecx
movl %eax, -4(%rbp) ## 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
## -- End function
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "dll\n"

.subsections_via_symbols

center tables in a markdown file

· One min read

center tables in a markdown file

<div align="center">

| Descriptoion | struct passwd member | POSIX.1 | FreeBSD 8.0 | Linux 32.0 | Mac OS X 10.6.8 | Solaris |
|:-----------------------------|:---------------------|:---------:|:-----------:|:----------:|:---------------:|:---------:|
| user name | char *pw_name | $\bullet$ | $\bullet$ | $\bullet$ | $\bullet$ | $\bullet$ |

_Figure 6.1 Fields in `/etc/passwd` file_
</div>

latex (math) support for markdown

· One min read

Math Equations

  • import tha plugins in docusaurus.config.js
const math = require('remark-math');
const katex = require('rehype-katex');
remarkPlugins: [math],
rehypePlugins: [katex],

under stylesheets

stylesheets: [
{
href: 'https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css',
type: 'text/css',
integrity:
'sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM',
crossorigin: 'anonymous',
},
],
  • install modules
$ npm install --save remark-math@3 rehype-katex@5 hast-util-is-element@1.1.0