Skip to content

Instantly share code, notes, and snippets.

View cpq's full-sized avatar
🎭
Купатися чи не купатись?

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / bsd.c
Created May 1, 2024 09:16
Final code for the "BSD socket API explained" video
// Source for the https://www.youtube.com/watch?v=pp9AM5A1mDs
// Written & tested on MacOS
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@cpq
cpq / Stack.md
Last active April 27, 2024 12:27
Why stack grows down

Why stack grows down

Any running process has several memory regions: code, read-only data, read-write data, et cetera. Some regions, such as code and read-only data, are static and do not change over time. Other regions are dynamic: they can expand and shrink. Usually there are two such regions: dynamic read-write data region, called heap, and a region called stack. Heap holds dynamic memory allocations, and stack is mostly used for keeping function frames.

Both stack and heap can grow. An OS doesn't know in advance whether stack or heap will be used predominantly. Therefore, an OS must layout these two memory regions in a way to guarantee maximum space for both. And here is the solution:

  1. Layout static memory regions at the edges of process's virtual memory
  2. Put heap and stack on edges too, and let them grow towards each other: one grows up, one grows down
<!DOCTYPE html>
<html lang="en">
<head>
<title>modal</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.0/css/bootstrap.min.css" />
</head>
<body></body>
@cpq
cpq / embed.c
Last active March 4, 2024 10:45
How to embed data files into C/C++ executable
// Copyright (c) Sergey Lyubka, 2013.
// All rights reserved.
// Released under the MIT license.
// This program is used to embed arbitrary data into a C binary. It takes
// a list of files as an input, and produces a .c data file that contains
// contents of all these files as collection of char arrays.
// Usage:
// 1. Compile this file:
// cc -o embed embed.c
@cpq
cpq / rt1060.sh
Last active December 4, 2023 14:00
Flashing rt1060-evk via vcon SWD
#!/bin/bash
# SWD commands to call rt1060 flash boot rom functions
DEVCALL="curl -su :$VCON_API_KEY https://dash.vcon.io/api/v3/devices/13"
# SWD exec: se COMMANDS. Hex addresses are without leading 0x
se() { $DEVCALL/rpc/swd.exec -d "{\"req\": \"$*\"}" | jq -cr .resp ; }
rm() { se rm,$1 | cut -d, -f2 ; }
dump() { $DEVCALL/rpc/swd.read -d "{\"addr\":$1, \"size\": $2}" | jq -r .data | xxd -r -p | hexdump -C ; }
hex() { node -p "($*).toString(16)"; }
@cpq
cpq / stm32.md
Last active September 26, 2023 09:56
STM32 naming

STM32 naming scheme

Example MCU: STM32 H743ZI

Example Type Description
H Type F: mainstream, L: low power, H: high performance, W: wireless
7 Core 0: M0, 1: M3, 2: M3, 3: M4, 4: M4, 7: M7
23 Line speed, peripherals, silicon process, …
Z Pin count F: 20, G: 28, K: 32, T: 36, S: 44, C: 48, R: 64,66, V: 100, Z: 144, I: 176
@cpq
cpq / Mutex.md
Last active May 14, 2023 18:15
What is a mutex and how does it work

What is a mutex and how does it work

Imagine a big office (your program), with many assets (shared resources) and many employees (threads). For example, all employees share one common resource - a toilet. They have agreed to use a label on a toilet's door (mutex).

When an employee wants to use the toilet he checks a label on a door (locks a mutex). If it is "engaged" he waits (blocks on a mutex), then when it is "free", he enters the toilet and changes the label to

@cpq
cpq / Hash.md
Created January 24, 2014 14:35
What is a hash and how does it work

What is a hash and how does it work

Let us start from a practical example. Imagine we are writing a traffic monitoring application. The purpose of application would be to calculate a number of bytes sent by each IP address. To do that, let create a structure that does that accounting:

struct ipstat {

uint32_t ip; /* Source IP address */

// Copyright (c) Cesanta Software Limited
// All rights reserved
// SPDX-License-Identifier: MIT
// Usage example (Arduino):
// char buf[100];
// struct slip slip = {.buf = buf, .size = sizeof(buf) - 1};
// ...
// unsigned char c = Serial.read();
// size_t len = slip_recv(c, &slip);
@cpq
cpq / queue2.c
Created February 15, 2023 09:26
#include <sttdef.h>
// Single producer, single consumer non-blocking queue
//
// Producer:
// void *buf;
// while (mg_queue_space(q, &buf, len) == 0) WAIT(); // Wait for free space
// memcpy(buf, data, len); // Copy data to the queue
// mg_queue_add(q, len); // Advance q->head
//