I am working with code that was written in VS 6.0. I am able to open the project in VS 2010 express. But when I compile it, it gives me the odd errors:
here the code that it's pointing to
#define AFX_CRT_ERRORCHECK(expr) do { expr; } while (0)
#if _SECURE_ATL
#ifdef _AFX
#define ATLMFC_CRT_ERRORCHECK(expr) AFX_CRT_ERRORCHECK(expr)
#else
#define ATLMFC_CRT_ERRORCHECK(expr) ATL_CRT_ERRORCHECK(expr)
#endif
...
inline errno_t __cdecl strncpy_s(__out_ecount(_SizeInChars) char *_Dest, __in size_t _SizeInChars, __in_z const char *_Source, __in size_t _Count)
{
return ATLMFC_CRT_ERRORCHECK(::strncpy_s(_Dest, _SizeInChars, _Source,_Count));
}
inline errno_t __cdecl wcsncpy_s(__out_ecount(_SizeInChars) wchar_t *_Dest, __in size_t _SizeInChars, __in_z const wchar_t *_Source, __in size_t _Count)
{
return ATLMFC_CRT_ERRORCHECK(::wcsncpy_s(_Dest, _SizeInChars, _Source,_Count));
}
inline errno_t __cdecl tcsncpy_s(__out_ecount(_SizeInChars) TCHAR *_Dest, __in size_t _SizeInChars, __in_z const TCHAR *_Source, __in size_t _Count)
{
#ifndef _ATL_MIN_CRT
return ATLMFC_CRT_ERRORCHECK(::_tcsncpy_s(_Dest, _SizeInChars, _Source,_Count));
#else
#ifdef UNICODE
return ATLMFC_CRT_ERRORCHECK(::wcsncpy_s(_Dest, _SizeInChars, _Source,_Count));
#else
return ATLMFC_CRT_ERRORCHECK(::strncpy_s(_Dest, _SizeInChars, _Source,_Count));
#endif
#endif
}
Related
I need an std::allocator<> compliant allocator with page-aligned allocation and preferrably no memory-pooling for very large allocations. Another requirement is that the allocator should have a non-standard reset()-method to reset a row of pages that the pages become discardable by the kernel if you have a row of trivial data types.
I wrote such an allocator which I post here below completely since I think others could use the allocator also if they also have this certain allocation constraints.
#pragma once
#if defined(_WIN32)
#include <Windows.h>
#elif defined(__unix__)
#include <unistd.h>
#include <sys/mman.h>
#else
#error platform not supported
#endif
#include <cstddef>
#include <type_traits>
#include <memory>
#include <utility>
#include <atomic>
#include <cassert>
#include <new>
#if !defined(NDEBUG)
#include <bit>
#endif
#include <utility>
template <typename T>
struct virtual_allocator
{
using allocator_type = virtual_allocator;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using propagate_on_container_move_assignment = std::false_type;
constexpr virtual_allocator() noexcept {}
constexpr virtual_allocator( virtual_allocator const & ) noexcept {}
static value_type *allocate( std::size_t n );
static void deallocate( value_type *p, std::size_t n ) noexcept;
static void reset( void *p, std::size_t n )
requires (sizeof(T) == std::bit_floor( sizeof(T) )) && std::is_trivial_v<T>;
static std::size_t get_page_size();
#if defined(__cpp_lib_allocate_at_least)
static std::allocation_result<value_type *> allocate_at_least( std::size_t n );
#endif
private:
using alloc_ret_t = std::pair<value_type *, std::size_t>;
static alloc_ret_t allocateAtLeast( std::size_t n );
static std::size_t getPageSize();
};
template <typename T>
typename virtual_allocator<T>::value_type *virtual_allocator<T>::allocate( std::size_t n )
{
return allocateAtLeast( n ).first;
}
template <typename T>
void virtual_allocator<T>::deallocate( value_type *p, std::size_t n ) noexcept
{
std::size_t pageSize = getPageSize();
assert(n * pageSize / pageSize == n);
n = n * pageSize + pageSize - 1 & -(ptrdiff_t)pageSize;
#if defined(_WIN32)
bool succ = (bool)VirtualFree( p, 0, MEM_RELEASE );
#elif defined(__unix__)
bool succ = !munmap( p, n );
#endif
assert(succ);
}
#if defined(__cpp_lib_allocate_at_least)
template <typename T>
std::allocation_result<typename virtual_allocator<T>::value_type *> virtual_allocator<T>::allocate_at_least( std::size_t n )
{
auto ret = allocateAtLeast( n );
return std::allocation_result<value_type *>( ret.first, ret.second );
}
#endif
template <typename T>
typename virtual_allocator<T>::alloc_ret_t virtual_allocator<T>::allocateAtLeast( std::size_t n )
{
using namespace std;
if( n * sizeof(value_type) / sizeof(value_type) != n )
throw bad_alloc();
size_t pageSize = getPageSize();
n = n * sizeof(value_type) + pageSize - 1 & -(ptrdiff_t)pageSize;
#if defined(_WIN32)
value_type *p = (value_type *)VirtualAlloc( nullptr, n, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
#elif defined(__unix__)
value_type *p = (value_type *)mmap( nullptr, n, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
#endif
if( !p ) [[unlikely]]
throw bad_alloc();
return alloc_ret_t( p, n / sizeof(value_type) );
}
template <typename T>
void virtual_allocator<T>::reset( void *p, std::size_t n )
requires (sizeof(T) == std::bit_floor( sizeof(T) )) && std::is_trivial_v<T>
{
using namespace std;
if( ((size_t)p | n) & getPageSize() - 1 || n * sizeof(value_type) / sizeof(value_type) != n ) [[unlikely]]
throw bad_alloc();
#if defined(_WIN32)
bool succ = (bool)VirtualAlloc( p, n, MEM_RESET, PAGE_READWRITE );
#elif defined(__unix__)
bool succ = !madvise( p, n, MADV_DONTNEED );
#endif
assert(succ);
}
template <typename T>
inline
std::size_t virtual_allocator<T>::get_page_size()
{
return getPageSize();
}
template <typename T>
inline
std::size_t virtual_allocator<T>::getPageSize()
{
using namespace std;
static atomic<size_t> aPageSize( 0 );
size_t pageSize = aPageSize.load( memory_order_relaxed );
if( !pageSize ) [[unlikely]]
{
#if defined(_WIN32)
SYSTEM_INFO si;
GetSystemInfo( &si );
pageSize = si.dwPageSize;
#elif defined(__unix__)
pageSize = sysconf( _SC_PAGESIZE );
#endif
assert(pageSize && pageSize == bit_floor( pageSize ));
aPageSize.store( pageSize, memory_order_relaxed );
}
return pageSize;
}
The testing code compiles fine with g++ 11.1.0 and clang++ 13 under Linux. Under Windows it compiles if you have a "Release" compile but not in "Debug" mode. I get some weird errors for which I don't have any clue what they're abut.
Severity Code Description Project File Line Suppression State
Error C2440 'static_cast': cannot convert from 'virtual_allocator<array_t>' to 'virtual_allocator<_Newfirst>' virtual_allocator C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\vector 595
Error C2530 '_Alproxy': references must be initialized virtual_allocator C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\vector 595
Error C3536 '_Alproxy': cannot be used before it is initialized virtual_allocator C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\vector 596
Error C2672 '_Delete_plain_internal': no matching overloaded function found virtual_allocator C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\vector 596
Error C2893 Failed to specialize function template 'void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept' virtual_allocator C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\vector 596
Although I think the code would be helpful for other C++-developers I don't post this with any C++-tags. That's while if others search such an allcator they'll find it by googling anyway and my question is only related to Visual C++.
Yeah, Igor got it ! It was the missing templated constructor.
template<typename T2>
constexpr virtual_allocator( virtual_allocator<T2> const & ) noexcept {}
I am using Flex&Bison in winCE 6.0.
There are some problem in errno.h.
In my SDK the errno.h is:
#ifndef _INC_ERRNO
#define _INC_ERRNO
#include <crtdefs.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Error Codes */
But the SDK of VC which also used in Flex is:
#ifndef _INC_ERRNO
#define _INC_ERRNO
#include <crtdefs.h>
#ifdef __cplusplus
extern "C" {
#endif
/* declare reference to errno */
#ifndef _CRT_ERRNO_DEFINED
#define _CRT_ERRNO_DEFINED
_CRTIMP extern int * __cdecl _errno(void);
#define errno (*_errno())
errno_t __cdecl _set_errno(__in int _Value);
errno_t __cdecl _get_errno(__out int * _Value);
#endif
/* Error Codes */
So,is it possible to avoid using errno in Flex?
Add some option? or privode myself header file instead of errno.h?
I wrote a small device driver for a "coin" device. I create an entry in /drivers/char/Kconfig
and corresponding Makefile, then selected built-in option in menuconfig. The kernel compiled fine (built-in.o file was created). But I still can't access the device (/dev/coin was not created) and there was no entry under /proc/devices.
Please help!!
I am cross-compiling for powerpc
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/random.h>
#include <linux/debugfs.h>
#include <linux/init.h>
#define DEVNAME "coin"
#define LEN 20
enum values {HEAD, TAIL};
struct dentry *dir, *file;
int file_value;
int stats[2] = {0, 0};
char *msg[2] = {"head\n", "tail\n"};
static int major;
static struct class *class_coin;
static struct device *dev_coin;
static ssize_t r_coin(struct file *f, char __user *b,
size_t cnt, loff_t *lf)
{
char *ret;
u32 value = random32() % 2;
ret = msg[value];
stats[value]++;
return simple_read_from_buffer(b, cnt,
lf, ret,
strlen(ret));
}
static struct file_operations fops = { .read = r_coin };
#ifdef CONFIG_COIN_STAT
static ssize_t r_stat(struct file *f, char __user *b,
size_t cnt, loff_t *lf)
{
char buf[LEN];
snprintf(buf, LEN, "head=%d tail=%d\n",
stats[HEAD], stats[TAIL]);
return simple_read_from_buffer(b, cnt,
lf, buf,
strlen(buf));
}
static struct file_operations fstat = { .read = r_stat };
#endif
static int __init coin_init(void)
{
void *ptr_err;
major = register_chrdev(0, DEVNAME, &fops);
if (major < 0)
return major;
class_coin = class_create(THIS_MODULE,
DEVNAME);
if (IS_ERR(class_coin)) {
ptr_err = class_coin;
goto err_class;
}
dev_coin = device_create(class_coin, NULL,
MKDEV(major, 0),
NULL, DEVNAME);
if (IS_ERR(dev_coin))
goto err_dev;
#ifdef CONFIG_COIN_STAT
dir = debugfs_create_dir("coin", NULL);
file = debugfs_create_file("stats", 0644,
dir, &file_value,
&fstat);
#endif
return 0;
err_dev:
ptr_err = class_coin;
class_destroy(class_coin);
err_class:
unregister_chrdev(major, DEVNAME);
return PTR_ERR(ptr_err);
}
static void __exit coin_exit(void)
{
#ifdef CONFIG_COIN_STAT
debugfs_remove(file);
debugfs_remove(dir);
#endif
device_destroy(class_coin, MKDEV(major, 0));
class_destroy(class_coin);
return unregister_chrdev(major, DEVNAME);
}
module_init(coin_init);
module_exit(coin_exit);
What if you manually insert module into kernel using insmod? Does it work? Any messages in dmesg?
As I remember entries in /dev (/dev/coin) should be created manually using mknod, but you need major number of registered device. Just printk it after register_chrdev().
#define SHMKEY ((long)12344)
#define SEMKEY ((long)12345)
#define MAXS 10000
#define MAXL 1000
typedef struct{
int lung;``
int i;
char s[MAXS];
}Mesaj;
#define PLUS (sizeof(int))
typedef struct{
int tip;
int pid;
Mesaj mesaj;
} Segment;
#include <string.h>
#include "shmmesaj.h"
struct sembuf s= {0, 1,0}; //clean segment
struct semibuf w= {0,-1,0}; //ocupy segment
void Wait(int semafor) {
semop(semafor,&w,1);
}
void Signal(int semafor) {
semop(semafor,&s,1);
}
Mesaj * ReadMes(Segment* segment){
static Mesaj mesaj;
mesaj.lung=segment->mesaj.lung;
memcpy((char*)&mesaj+sizeof(int), (char*)segment+3*sizeof(int), mesaj.lung);
return &mesaj;
}
void WriteMes(Segment* segment,Mesaj * pm){
segment->mesaj.lung=pm->lung;
memcpy((char*)segment+3*sizeof(int),(char*)pm+sizeof(int),pm->lung);
}
main(){}
I'm trying to write a simple char device for linux, I need to read and write the device by fread/fwrite and use fopen and fseek. I've written a simple test program to use my device and I've noted that fpos in fseek function doesn't work and unknow fread after fseek appears. I cannot get the device working because fseek doesn't get a correct file position when opened in r+ mode, everything works except fread if I use file opened in w mode instead.
Thanks folks
/* Makefile */
obj-m += char.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
/* char device char.c */
// to complie:
// # make
// # insmod char.ko
// # dmesg
// (to get majornumber)
// # mknod /dev/dp0 c majornumber 0
// # ./test
// # dmesg
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
int char_init (void);
void char_exit (void);
int char_open (struct inode *, struct file *);
int char_release (struct inode *, struct file *);
ssize_t char_read (struct file *, char __user *, size_t, loff_t *);
ssize_t char_write (struct file *, const char __user *, size_t, loff_t *);
loff_t char_llseek (struct file *, loff_t, int);
int char_open (struct inode *inode, struct file *fp)
{
printk ("f_mode=%d f_pos=%d\n", (int) fp->f_mode, (int)fp->f_pos);
return 0;
}
int char_release (struct inode *inode, struct file *fp)
{
return 0;
}
ssize_t char_read (struct file *fp, char __user *buff, size_t count, loff_t *fpos)
{
printk ("char_read() fp->f_pos=%d\n", (int) fp->f_pos);
return count;
}
ssize_t char_write (struct file *fp, const char __user *buff, size_t count, loff_t *fpos)
{
printk ("char_write() fp->f_pos=%d\n", (int) fp->f_pos);
return count;
}
loff_t char_llseek (struct file *fp, loff_t fpos, int whe)
{
printk ("char_llseek() fpos=%d\n ", (int) fpos);
fp->f_pos = fpos;
return fpos;
}
struct file_operations char_fops =
{
.owner = THIS_MODULE,
.open = char_open,
.release = char_release,
.read = char_read,
.write = char_write,
.llseek = char_llseek,
.ioctl = NULL,
};
/* the char device */
struct cdev cdev;
int char_init ()
{
dev_t mm;
int mj, mi, ret;
mi = 0;
ret = alloc_chrdev_region (&mm, mi, 1, "dp0");
mj = MAJOR(mm);
cdev_init (&cdev, &char_fops);
ret = cdev_add (&cdev, mm, 1);
if (ret<0)
{
printk ("char: unable to add device\n");
return -1;
}
printk ("char: device added major=%d\n", mj);
return 0;
}
void char_exit ()
{
cdev_del(&cdev);
printk ("char: device deleted\n");
}
module_init(char_init);
module_exit(char_exit);
/* test program test.c */
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[])
{
int ret;
FILE *fp;
fp = fopen ("/dev/dp0", "r+");
if (fp == NULL)
{
printf ("open failed\n");
return -1;
}
int x;
x= fseek (fp, 1, SEEK_SET);
printf("ret fseek=%d\n", x);
fseek (fp, 1, SEEK_SET);
fwrite ("\0", 1, 1, fp);
fseek (fp, 2, SEEK_SET);
fread (&ret, 1, 1, fp);
printf ("DONE\n");
fclose (fp);
return 0;
}
// this is my dmesg:
f_mode=31 f_pos=0 // this is my fopen
char_llseek() fpos=0 // this is my first fseek but why fpos=0? it should be 1
char_read() fp->f_pos=0 // what's that?
char_llseek() fpos=0 //this is my second fseek but why fpos is 0 again?
char_read() fp->f_pos=0 // I suppose this is my fread fpos=0?
char_llseek() fpos=-4095 // what's that?
char_llseek() fpos=-4095 // What's that too?
char_llseek() fpos=-4095 // What's that too?
You are using stdio (fread, fseek, etc.) which generally has a buffer of sorts. It may not necessarily translate directly to the syscalls, hence the "unexpected" reads and seeks.