小编St.*_*rio的帖子

如何跨多个翻译单元共享opaque类型?

我正在开发自己的C项目,需要一些design/common_C_idiom建议.我试图解决的问题是打开已配置设备的输入日期流.但我想保持配置和设备分离.这是我尝试过的:

  1. 组态:

config.h

#ifndef CONFIG_H
#define CONFIG_H

typedef struct config_t config_t;

config_t* config_t_allocate(void);
void config_t_free(config_t *);
//functions to set configuration parameters

#endif //CONFIG_H
Run Code Online (Sandbox Code Playgroud)

config.c

#include <stdlib.h>
#include "device.h"

struct config_t{
    const char * name;
};

config_t* config_t_allocate(void){
    return malloc(sizeof(config_t));
}

void config_t_free(config_t * config_ptr){
    free(config_ptr);
}
Run Code Online (Sandbox Code Playgroud)
  1. 设备:

device.h

#ifndef DEVICE_H
#define DEVICE_H

typedef struct config_t config_t;

typedef struct device_t device_t;

void configure_device(device_t**, config_t*);

//other device-related methods

#endif //DEVICE_H
Run Code Online (Sandbox Code Playgroud)

device.c

#include "device.h"
#include <sys/fcntl.h>

struct device_t{
    int fd;
};

//Does not …
Run Code Online (Sandbox Code Playgroud)

c linux struct

0
推荐指数
1
解决办法
56
查看次数

What is the actual size of stack red zone?

In the x86-64 System V ABI it is specified that the space behind the $rsp - 128 is the so-called red zone which is not touched by any signal handlers. On my machine

$ ulimit -s
8192
Run Code Online (Sandbox Code Playgroud)

I expected there is only 2 pages in the stack. So I wrote the following program to test till which size red zone can expand:

PAGE_SIZE equ 0x1000
SYS_exit equ 0x3C

section .text
global _start 

_start:
    lea rcx, [rsp - 0x1f * PAGE_SIZE] …
Run Code Online (Sandbox Code Playgroud)

assembly callstack mmap x86-64 red-zone

0
推荐指数
1
解决办法
125
查看次数

如果涉及 MAP_FIXED,无限堆栈不能超过最初的 132KiB?

我正在使用堆栈进行一些实验,以下内容让我陷入困境。

可以看出Linux在大小上有初始[stack]映射132KiBulimit -s unlimited如果我们进行相应的调整,我们可以进一步扩展堆栈rsp。所以我设置ulimit -s unlimited并运行了以下程序:

PAGE_SIZE     equ 0x1000

;mmap staff
PROT_READ     equ 0x01
PROT_WRITE    equ 0x02
MAP_ANONYMOUS equ 0x20
MAP_PRIVATE   equ 0x02
MAP_FIXED     equ 0x10

;syscall numbers
SYS_mmap      equ 0x09
SYS_exit      equ 0x3c

section .text

global _start

_start:
    ; page alignment
    and rsp, -0x1000

    ; call mmap 0x101 pages below the rsp with fixed mapping
    mov rax, SYS_mmap
    lea rdi, [rsp - 0x101 * PAGE_SIZE]
    mov rsi, PAGE_SIZE
    mov …
Run Code Online (Sandbox Code Playgroud)

linux assembly callstack mmap x86-64

0
推荐指数
1
解决办法
69
查看次数

为什么我的“ = r”(var)输出没有选择与“ a”(var)输入相同的寄存器?

我正在学习如何__asm__ volatile在GCC中使用,并提出了一个问题。我想实现一个执行原子比较和交换并返回先前存储在目标中的值的函数。

为什么"=a"(expected)输出约束起作用,但是"=r"(expected)约束使编译器生成不起作用的代码?

情况1。

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

uint64_t atomic_cas(uint64_t * destination, uint64_t expected, uint64_t value){
    __asm__ volatile (
        "lock cmpxchgq %3, %1":
        "=a" (expected) :
        "m" (*destination), "a" (expected), "r" (value) :
        "memory"
    );

    return expected;
}

int main(void){
    uint64_t v1 = 10;
    uint64_t result = atomic_cas(&v1, 10, 5);
    printf("%" PRIu64 "\n", result);           //prints 10, the value before, OK
    printf("%" PRIu64 "\n", v1);               //prints 5, the new value, OK
}
Run Code Online (Sandbox Code Playgroud)

它按预期工作。现在考虑以下情况: …

c x86 assembly gcc inline-assembly

0
推荐指数
1
解决办法
82
查看次数

如何遍历向量?

我需要严格按照元素被推回向量中的顺序进行迭代。对于我的特殊情况,最好使用迭代器,而不是像下面那样通过for-each循环进行迭代:

std::vector<int> vector;
for(int i = 0; i < vector.size(); i++)
   //not good, but works
Run Code Online (Sandbox Code Playgroud)

我的问题是,是否真的可以像这样通过迭代器遍历向量:

std::vector<int> v;
for(typename std::vector<int>::iterator i = v.iterator(); i != v.end(); i++)
   //good, but I'm not strictly sure about the iterating order.
Run Code Online (Sandbox Code Playgroud)

因此,我可以根据需要安全地使用迭代器吗?它是标准的吗?

c++ iterator vector

-1
推荐指数
1
解决办法
6258
查看次数

丢弃const限定符

为什么不允许丢弃const限定符?假设我们写道:

#include <iostream>

struct A
{
    void operator=(const A&){ std::cout << "A&" << std::endl; }
    void operator=(const A&&){ std::cout << "A&&" << std::endl; }
};

const A a;
A b;

int main()
{
    a = b; //Error: discarding qualifier
}
Run Code Online (Sandbox Code Playgroud)

有人不能提供标准不允许的参考吗?

c++ class operator-keyword

-2
推荐指数
1
解决办法
110
查看次数

NASM 代表 nop 组装为暂停

考虑以下用 x86 汇编语言编写的函数

foo:
        rep
        nop
        ret
Run Code Online (Sandbox Code Playgroud)

使用 NASM 汇编代码并反汇编它,gdb我们有:

(gdb) disas foo
Dump of assembler code for function foo:
   0x0000000000000610 <+0>:     pause  ;pause ????
   0x0000000000000612 <+2>:     ret    
   0x0000000000000613 <+3>:     nop    WORD PTR cs:[rax+rax*1+0x0]
   0x000000000000061d <+13>:    nop    DWORD PTR [rax]
End of assembler dump.
Run Code Online (Sandbox Code Playgroud)

它使用pause原始程序集中未提供的指令。为什么会这样?这是 NASM 故意记录的行为吗?那么如果一些早期的 x86cpu没有pause我们可以直接使用rep nop吗?

x86 assembly gdb x86-64 nasm

-2
推荐指数
1
解决办法
361
查看次数