What do you pass to a method wait(int*) - iphone

When I simply pass the int I get the warning:
WARNING: Incompatible integer to pointer conversion passing int to parameter of
type int *
In another words what is int *

This requires the address of an integer variable, not the integer variable itself. You can get the address of a variable with the & operator. So the following code would work:
int i = 10;
wait( &i );

The man page for wait() describes what the argument is for:
pid_t
wait(int *stat_loc);
The wait() function suspends execution of its calling process until stat_loc information is available for a terminated child process, or a signal is received. On return from a successful wait() call, the stat_loc area contains termination information about the process that exited as defined below.
The * indicates that a pointer is desired. Since arguments are passed by value to function calls, the way a value is returned via a function parameter is through a pointer to the object receiving the value.
int status;
pid_t p;
p = wait(&status);

Related

Error: passing 'const xxx' as 'this' argument discards qualifiers

I am trying to implement bigint class in c++, it's not completed yet, i have encountered some errors that i am unable understand.
I have erased all other functions (as they are unnecessary in this case)
and karatsuba is not yet completed (but that should't pose a problem in this case).
In the multiply function (overloaded * ) my compiler gives an error:
passing 'const BigInt' as 'this' argument discards qualifiers [-fpermissive]
at line
ans.a = karatsuba(n,m);
I understand that this would occur when i am trying to change a constant object or object passed to a constant function, in my case i am merely creating a new vector and passing it to karatsuba function.
Removing const from overloded * gets rid of this error.
So,does this mean that a constant function can't change anything at all? (including local variables?)
class BigInt {
typedef long long int ll;
typedef vector<int> vi;
#define p10 1000000000;
#define range 9
vi a;
bool sign;
public:
BigInt operator * (const BigInt &num) const
{
vi n(a.begin(),a.end()),m(num.a.begin(),num.a.end());
BigInt ans;
ans.sign = !(sign ^ num.sign);
while(n.size()<m.size()) n.push_back(0);
while(n.size()>m.size()) m.push_back(0);
ans.a = karatsuba(n,m);
return ans;
}
vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}
};
Ok so after googling a bit more, i realized that this pointer is implicitly passed to the oveloaded * and then on to karatsuba (as it is a member function of the class), and as karatsuba is not a constant function, there is no guarantee that it won't change the object contents, hence this error is triggered.
One solution is to declare karatsuba as static, as static member functions don't receive this pointer (they can even be called with out a class object simply using :: operator) , read more about them from here Static data members and member functions.
All that is needed to be changed is :-
static vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}

Systemverilog const ref arg position when constructing an object

I am creating a class that needs a reference to the test bench's configuration object. Since the configuration must be intact throughout the simulation, I pass it as a const ref object. Here is a sudo code that I want to run:
class tb_config;
int unsigned rate;
int unsigned chnls[];
const int unsigned nb_chnls;
function new (int unsigned rate, int unsigned nb_chnls);
this.rate = rate;
this.nb_chnls = nb_chnls;
chnls = new[nb_chnls];
endfunction
endclass
class tx_phy;
static int phy_id;
tb_config cfg;
function new (int unsigned phy_id, const ref tb_config cfg);
this.phy_id = phy_id;
this.cfg = cfg;
endfunction
endclass
module test;
tb_config cfg = new(100, 4);
tx_phy phy = new( 1234, cfg);
endmodule
The code above works perfectly fine and it meets my expectation. But if I change the arguments in tx_phy::new to function new (const ref tb_config cfg, int unsigned phy_id); and pass the values to the constructor accordingly I get the following error in Cadence Incisive:
invalid ref argument usage because actual argument is not a variable.
Also same thing happens when I test it with Aldec in edaplayground: https://www.edaplayground.com/x/5PWV
I assume this is a language limitation, but is there any other reason for that??
The reason for this is because the argument kind is implicit if not specified. You specified const ref for the first argument, but nothing for the second argument, so it is also implicitly const ref. Adding input to the second argument declaration fixes this.
function new (const ref tb_config cfg, input int unsigned phy_id);
I also want to add const ref tb_config cfg is equivalent to writing
function new (tb_config cfg, int unsigned phy_id);
Both of these arguments are implicitly input arguments, which means they are copied upon entry.
A class variable is already a reference. Passing a class variable by ref means that you can update the handle the class variable has from within the function. Making the argument a const ref means you will not be able to update the class variable, but you can still update members of the class the variable references. There is no mechanism to prevent updating members of class object if you have a handle to it other than by declaring them protected or local.
The only place it makes sense to pass function arguments by ref in SystemVerilog is as an optimization when the arguments are large data structures like an array, and you only need to access a few of the elements of the array. You can use task ref arguments when the arguments need to be updated during the lifetime of the task (i.e. passing a clock as an argument).

Updating a classes' variable in a constructor through pass by reference?

Blazing ahead with newfound knowledge of SystemVerilog's inner workings I've set out to use one of these fandangled pass-by-reference features to update a classes' counter in the constructor of another class. The setup (stripped to the basics) looks somewhat like this:
class my_queue;
int unsigned num_items; //Want to track the number of items this Queue has seen.
function push_new_item();
item new_item = new(num_items);
endfunction
endclass
class parent_item;
int unsigned x_th_item;
function new(ref int unsigned num_items);
x_th_item = num_items;
num_items += 1; //This should increase the counter in num_items.
endfunction
endclass
class item extends parent_item;
function new(ref int unsigned num_items);
super.new(num_items);
endfunction
endclass
The issue is that my compiler is complaining about an
Illegal connection to the ref port 'num_items' of function/task parent_item::new, formal argument should have same type as actual argument.
I have an idea on how to fix this: Moving the increment after the call to new() in push_new_items.
But then I still won't know how to correctly use pass-by-refrence in SV so what's causing the error?
Is it the other pass-by-reference or maybe a syntactical error?
You do not need ref semantics for this, use an inout argument.
inout's are copied-in upon entry and copied-out upon return of a task or function. The type compatibility requirements are much stricter as you have seen for ref arguments.
The only occasion you must use a ref argument isin time consuming tasks and you need to see active updates to the arguments before the task returns.
task my_task(ref bit tclock);
#(posedge tclock) // this would hang if tclock was an input
endtask
Another place you might want to use a ref argument is as an optimization when the argument type is a large object like an array. But passing a single int by reference is actually slower than copying its value directly.
Qiu did point me to the issue with my code. My problem was that, whilst the variables were declared correctly on both ends, one of my constructors was written:
function new(ref int num_items);
where it should have rather been
function new(ref int unsigned num_items);
Thank you Qiu.

Callback from python

After reading a lot from this wonderful web, I need some help.
I have this function (in a dll):
kvStatus kvSetNotifyCallback ( const int hnd, kvCallback_t callback, void * context,
unsigned int notifyFlags);
How to call this function from Python?
I tried:
kvSetNotifyCallback(c_int(hnd1),c_void_p(self.can_rx()),None, c_int(canNOTIFY_RX))
def can_rx(self):
print("OK")
The function can_rx does execute only once. Any suggestions? Thank you very much.
MM
first thing:
The argtypes of ctypes WINFUCNTYPE or CFUNCTYPE should be a c_void_p
The argtypes of fucntion which you are defining should be py_object instead of a c_void_p
so that you can pass objects of structures and arrays as well.
then in the callback fucntion cast the void_p data as
ct.cast(userdata,ct.py_object).value
second thing:
code running only once: put a return value at bottom of the code.
return 1 or return TRUE
example:
CALLBACKFUNC = WINFUNCTYPE("returnvalue",c_int,c_void_p)
kvSetNotifyCallback.argtypes = [c_int,CALLBACKFUNC,py_object,c_uint]
then in the fucntion:
def can_rx(arg1,arg2):
arg3 = cast(arg2,py_object).value #yields object value
print arg1
print arg3
return 1 #for continuing execution
The CALLBACK FUNCTION can_rx should be called at CALLBACKFUNC(can_rx), the data to be passed along with arg1 should be passed at py_object position.
so you should call your callback as:
kvSetNotifyCallback(somevalue,CALLBACKFUC(can_rx),arg2,canNOTIFY_RX)
arg2 is the value to be passed to the callback func can_rx
can_rx can be defined as a global function also.

PowerRegisterSuspendResumeNotification - provided callback function doesn't work as expected

I register my application to receive notification when the system is suspended or resumed.
MSDN documentation
Function I'd like to be executed after application receives notification (I tried both void and void CALLBACK and both work same way):
void isConnectedStandby()
{
printf( "ConnectedStandby Request");
}
1st case - I provide pointer to the isConnectedStandby function, but system treats as a double pointer to the function - it calls an address which is under this callback pointer.
HPOWERNOTIFY RegistrationHandle;
PowerRegisterSuspendResumeNotification(
DEVICE_NOTIFY_CALLBACK,
&isConnectedStandby,
&RegistrationHandle
);
2nd case - here I provide as follows (this way my function code is executed):
typedef void (*StatusFunction_t)();
StatusFunction_t StatusFunction = isConnectedStandby;
HPOWERNOTIFY RegistrationHandle;
PowerRegisterSuspendResumeNotification(
DEVICE_NOTIFY_CALLBACK,
&isConnectedStandby,
&RegistrationHandle
);
System calls not only mine function, but all addresses after the first one (if I provide an array of functions, it executes one after another to crash when there is no valid code available)
What is the correct way to use this function?
Function declaration (must be static ULONG with 3 parameters as you can see below):
static ULONG isConnectedStandby(PVOID Context, ULONG Type, PVOID Setting);
ULONG isConnectedStandby(PVOID Context, ULONG Type, PVOID Setting)
{
printf( "ConnectedStandby Request");
return 0;
}
Istead of providing callback function directly to PowerRegisterSuspendResumeNotification we have to provide struct _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS filled with our functions address :
static _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS testCallback = {
isConnectedStandby,
nullptr
};
HPOWERNOTIFY RegistrationHandle;
PowerRegisterSuspendResumeNotification(
DEVICE_NOTIFY_CALLBACK,
&testCallback,
&RegistrationHandle
);
MSDN documentation did not mention any of those information.